'How can I input negative number without a string error in Python (The input must be in tuples) [closed]
def eqQuadratic(Variables):
valuesoflinear=Variables[1]
a = float(valuesoflinear[0])
b = float(valuesoflinear[2])
c = float(valuesoflinear[4])
x = float(valuesoflinear[6])
y = a*x**2 + b*x + c
return y
input2 = input("input a, x, b, c to calculate the quadratic equation ")
Variables = ("The input for quadratic equation are ", input2)
y = eqQuadratic(Variables)
print("the answer for quadratic equation is ", y)
If I input all positive numbers it works, but if I input negative numbers, it won't works.
I get this error
Traceback (most recent call last): File "C:/Users/maxime/Desktop/SESAME/PycharmProjects/Neocortex0.0/dj.py", line 14, in y = eqQuadratic(Variables) File "C:/Users/maxime/Desktop/SESAME/PycharmProjects/Neocortex0.0/dj.py", line 3, in eqQuadratic a = float(valuesoflinear[0]) ValueError: could not convert string to float: '-'
How can I have the 4 different values from the input()command and convert them into float number.
Solution 1:[1]
The error is that you don't ask for 4 number. so the valuesoflinear[0] is just a sign -. You need to ask 4 number that can be negartive like -1
here a solution :
def eqQuadratic(Variables):
a = float(Variables[0])
b = float(Variables[1])
c = float(Variables[2])
x = float(Variables[3])
y = a * x ** 2 + b * x + c
return y
inputa = input("input a to calculate the quadratic equation ")
inputx = input("input x to calculate the quadratic equation ")
inputb = input("input b to calculate the quadratic equation ")
inputc = input("input c to calculate the quadratic equation ")
# Variables = ("The input for quadratic equation are ", input2)
y = eqQuadratic([inputa,inputx,inputb,inputc])
print("the answer for quadratic equation is ", y)
You can ask for several input in a row but you have to be careful if less or more than 4 parameters are enter
def eqQuadratic(Variables):
a = float(Variables[0])
b = float(Variables[1])
c = float(Variables[2])
x = float(Variables[3])
y = a * x ** 2 + b * x + c
return y
input2 = input("input a, x, b, c to calculate the quadratic equation ").split()
# Variables = ("The input for quadratic equation are ", input2)
if not len(input2) == 4:
print('you need to enter 4 param')
else:
y = eqQuadratic(input2)
print("the answer for quadratic equation is ", y)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
