您应该使用int()
orfloat()
将用户输入的数字转换为整数/浮点数,然后将公式应用于它们。
need == "pythagoras" or "Pythagoras"
相当于:
(need == "pythagoras") or "Pythagoras"
因此,如果need
等于"pythagoras"
,则返回True
else 返回"Pythagoras"
(即 True 值),换句话说,无论输入是什么,您的if
条件始终是。True
工作代码:
need = raw_input("What do you need to Use?")
#use a while loop loop, this will continuously ask for the user input
#until he doesn't enters a corrects one.
while need.lower() != "pythagoras":
print "Invalid choice! try again"
need = raw_input("What do you need to Use?")
pythagoras = raw_input("What side do you Need?")
if pythagoras.lower() == "hypotenuse":
k1 = int(raw_input("Known Side 1: ")) #use int() to convert the user input to integers
k2 = int(raw_input("Known Side 2: ")) # use float() if you want floats
print (k1**2 + k2**2)**0.5 # now apply the formula