I was Trying to Make a Simple Maths Calculator.
pi = math.pi
e = math.e
φ = 1.618033988749895
while True:
sh= input("MATHS CALCULATOR: ")
if sh== "Values":
va=input("NAME OF VALUE: ")
if va=="pi":
print("PI value is", pi)
elif va=="PI":
print("PI value is", pi)
elif va=="e":
print("Euler's Number(e) value is", e)
elif va=="g":
print("Golden Ratio(φ) value is", φ)
else:
print("Try Another")
if sh== "Simple":
sim=input("ENTER AN OPERATOR: ")
a=int(input("ENTER FIRST NUMBER: "))
b=int(input("ENTER SECOND NUMBER: "))
if sim=="+":
c=a+b
print(a,"PLUS",b,"EQUALS",c)
elif sim=="*":
c=a*b
print(a,"TIMES",b,"EQUALS",c)
elif sim=="x":
c=a*b
print(a,"TIMES",b,"EQUALS",c)
elif sim=="/":
c=a/b
print(a,"BY",b,"EQUALS",c)
elif sim=="-":
c=a-b
print(a,"MINUS",b,"EQUALS",c)
else:
print("Try Another")
if sh=="Stop":
print("Thanks For Using!")
quit()
else:
print("Check Again the Spelling!")
I tried Everything from changing The entire Program. I also Tried to Remove the loop, Nothing Worked. All lead to this. (https://i.sstatic.net/xUVXP.png) I expect the code to only Show the 'ELSE' message when Nothing Matches.
the main if
s are uncorrelated in that way, meaning that every one is checked separately from the others. The last else is only combined with the last if
. To solve your problem, you should do something like this:
pi = math.pi
e = math.e
φ = 1.618033988749895
while True:
sh= input("MATHS CALCULATOR: ")
if sh== "Values":
va=input("NAME OF VALUE: ")
if va=="pi":
print("PI value is", pi)
elif va=="PI":
print("PI value is", pi)
elif va=="e":
print("Euler's Number(e) value is", e)
elif va=="g":
print("Golden Ratio(φ) value is", φ)
else:
print("Try Another")
elif sh== "Simple":
sim=input("ENTER AN OPERATOR: ")
a=int(input("ENTER FIRST NUMBER: "))
b=int(input("ENTER SECOND NUMBER: "))
if sim=="+":
c=a+b
print(a,"PLUS",b,"EQUALS",c)
elif sim=="*":
c=a*b
print(a,"TIMES",b,"EQUALS",c)
elif sim=="x":
c=a*b
print(a,"TIMES",b,"EQUALS",c)
elif sim=="/":
c=a/b
print(a,"BY",b,"EQUALS",c)
elif sim=="-":
c=a-b
print(a,"MINUS",b,"EQUALS",c)
else:
print("Try Another")
elif sh=="Stop":
print("Thanks For Using!")
quit()
else:
print("Check Again the Spelling!")
In this way, only one of the sh ==
/else
condition is accepted.