Reputation: 3071
Just started python a few hours ago and stumbles across a problem. The blow snippet shows that initially I store a userInput as 1 or 2 then execute a if block. Issue is that the code jumps straight to else (even if i type 1 in the console window). I know Im making a simple mistake but any help will be appreciated.
using Python 3.5 === running in Visual Studio 2015 === specifically cPython
userInput = float(input("Choose 1 (calculator) or 2 (area check)\n"))
if(userInput =='1') :
shape = input("Please enter name of shape you would like to calculate the area for\n")
if(shape == 'triangle') :
base = int(input("Please enter length of BASE\n"))
height = int(input("Please enter HEIGHT\n"))
print("The area of the triangle is %f" % ((base * height)*0.5))
elif (shape == 'circle') :
radius = int(input("Please enter RADIUS\n"))
print("The Area of the circle is %f" % ((radius**2)*22/7))
elif (shape == 'square') :
length = int(input("Please Enter LENGTH\n"))
print("The area of the square is %f" % ((length**2)*4))
else :
initial1 = float(input("Please enter a number\n"))
sign1 = input("please enter either +,-,*,/ \nwhen you wish to exit please type exit\n")
initial2 = float(input("Please enter number 2\n"))
if(sign1 == '+') :
answer = float(initial1) + float(initial2)
print(answer)
elif(sign1 == '*') :
answer = float(initial1) * float(initial2)
print(answer)
elif(sign1 == '-') :
answer = float(initial1) - float(initial2)
print(answer)
elif(sign1 == '/') :
answer = float(initial1) / float(initial2)
print(answer)
PS. if [possible] could you keep the help as basic as possible as I wanna make sure i understand the basics perfectly.
Thanks for all the help!! :D
Upvotes: 1
Views: 62
Reputation: 16993
You are converting your input to float but checking for the string of the number. Change it to:
If userInput == 1.0:
Or better yet, keep it the way it is and just don't convert your user input to float in the first place. It is only necessary to convert the input to float
or int
if you want to do math on it. In your case you are just using it as an option, so you can keep it as a string:
userInput = input("Choose 1 (calculator) or 2 (area check)\n")
P.S. make sure to be very careful with indentation in Python. I assume your indentation is correct in your code editor, but also take care when pasting into this site. You have everything on the same level here, but some of your if
blocks will need to be indented further in order for your program to work properly.
Upvotes: 1