+ 2
Why's there an error
I need help with the calculator that im making and there's an error in line 4 and idk why https://sololearn.com/compiler-playground/cOuhypuQs3Cs/?ref=app
12 Answers
+ 2
Abdulrahman alturk
+,-,*,/ are strings in your code so they should be enclosed with quotations.
And it's "==" not "=" while comparing.
+ 7
Abdulrahman alturk ,
just allow me a comment.
the second line (that is used for an operator sign): equation_type = str(input())
does not require the str() function, since all inputs in python are strings by default.
so we can just use: equation_type = input()
+ 2
+ is not a string. "+" is a string. same goes for the other operators in the comparisons.
+ 2
Guys ok i get it thanks guys
+ 1
still no difference
+ 1
Thanks 😊
+ 1
a = float(input())
equation_type = str(input())
b = float(input())
solve=0
#or you can save it in a list (solve=0)
if equation_type == '+' :
solve = a+b
elif equation_type == '-' :
solve = a-b
elif equation_type == '*' :
solve = a*b
elif equation_type == '/' :
solve = a/b
elif equation_type == '^' :
solve = a**b
print()
print()
print("_____________________________________")
print("the answer is equal to" + " " + str(solve))
“Solve “ is not Not defined ٫ You must define it in advance so that the answer can be stored in it and then displayed.
Line 4
And in this program you should write like this For example:
2
+
2
And it will give you the answer
+ 1
Take all the input in one line. Like this :
a, equation_type, b = input("Enter a operator b: ").split()
a = float(a)
b = float(b)
# There will be no error now
0
Use two = symbols in if statements. By the way if you wanna get advanced in python then you should try making a complex project like (this code is totally not fake)
https://sololearn.com/compiler-playground/c6l8CNwt0gTZ/?ref=app
0
Use this method
def addition(x, y):
return x + y
def soustraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
if y == 0:
return "Erreur : Division par zéro !"
return x / y
def calculatrice():
print("Calculatrice simple")
print("1. Addition")
print("2. Soustraction")
print("3. Multiplication")
print("4. Division")
print("5. Quitter")
while True:
choix = input("Choisissez une option : ")
if choix == "1":
x = float(input("Entrez le premier nombre : "))
y = float(input("Entrez le deuxième nombre : "))
print(f"{x} + {y} = {addition(x, y)}")
elif choix == "2":
x = float(input("Entrez le premier nombre : "))
y = float(input("Entrez le deuxième nombre : "))
print(f"{x} - {y} = {soustraction(x, y)}")
elif choix == "3":
x = float(input("Entrez le premier nombre : "))
0
This is not java, there's no need to make extra boilerplate "methods"