我最近开始学习编程,更具体地说是python。我是一个初学者,我开始研究我的第一个转换英寸和磅的小项目。我无法弄清楚为什么函数没有在if语句中调用。请帮忙!感谢。
# Convert inches to centimeters and pounds to kilograms
# Converts inches to centimeters
def convic():
amti = raw_input("How many inches? **Integers only!** ")
if amti.isdigit():
amti = int(float(amti))
centi = amti * 2.54
print "%d inches equals %d centimeters." % (amti, centi)
else:
print "Sorry that is not a number and/or integer."
# Converts pounds to kilograms
def convpk():
amtp = raw_input("How many pounds? **Integers only!** ")
if amtp.isdigit():
amtp = int(float(amtp))
kilo = amtp * 0.45359
print "%d pounds equals %d kilograms." % (amtp, kilo)
else:
print "Sorry that is not a number and/or integer."
answer = raw_input("Would you like to convert inches to centimeters or pounds to kilograms? ")
if answer == "inches to centimenters":
convic()
if answer == "pounds to kilograms":
convpk()
# Not a correct response
else:
while answer != "inches to centimeters" and "pounds to kilograms":
answer = raw_input("Sorry please enter inches to centimeters or pounds to kilograms. ")
if answer == "inches to centimenters":
convic()
if answer == "pounds to kilograms":
convpk()
答案 0 :(得分:1)
这一行不是你想要的:
while answer != "inches to centimeters" and "pounds to kilograms":
我想你想要:
while answer != "inches to centimeters" and answer != "pounds to kilograms":
答案 1 :(得分:1)
你似乎拼错了厘米作为厘米。 如果你纠正了那就应该有效。