仍然无法想象为什么格兰德将完全通过,但执行和初级不会?我已经尝试删除返回但是然后导致if语句相互碰撞。
def main(): # program is too ask subject about the hotel room he wants
yes = "yes" #Also calculate the cost of it
yes1 = "yes "
no = "no"
no1 = "no "
discount = .2
print("Welcome to Flappies Hotel!")
print("With a *three* night stay we will take 20% off your purchase.")
print("The suites we have available are the Junior, Executive, and Grande.")
print("The Grande suite runs at 350.00 a night.")
grande = input("Would you like the Grande suite? ")
if grande == yes or yes1 : #yes1 incase the user puts a space after the yes
grande = 350.00
else:
grande1 = int(float(input("How many nights do you intend to stay? ")))
grande2 = (grande*grande1)
if grande1 == 3.0 :
grande_total = (grande*grande1*discount)
grande = float(grande2-grande_total)
grande_fin = format(grande, '.2f')
print("Your total purchase will be",grande_fin,",enjoy your stay at Flappies.")
return #used to cut off the if statement so it wont continue on
else:
grande = 350.00
grande_total = float(grande*grande1)
grande_fin = format(grande_total, '.2f')
print("Your total purchase will be",grande_fin,",enjoy your stay at Flappies.")
return
print("The Executive suite runs 250.00 a night.")
executive = input("Would you like the Executive suite? ")
yes = "yes"
yes1 = "yes "
no = "no"
no1 = "no "
discount = .2
if executive == yes or yes1 :
executive = 250.00
else:
executive1 = int(float(input("How many nights do you intend to stay? ")))
executive2 = (executive*executive1)
if executive1 == 3.0 :
executive_total = (executive*executive1*discount)
executive = float(executive2-executive_total)
executive_fin = format(executive, '.2f')
print("Your total purchase will be",executive_fin,",enjoy your stay at Flappies.")
return
else:
executive = 250.00
executive_total = float(executive*executive1)
executive_fin = format(executive_total, '.2f')
print("Your total purchase will be",executive_fin,",enjoy your stay at Flappies.")
return
print("The Junior suite runs 150.00 a night.")
junior = input("Would you like the Junior suite? ")
if junior == yes or yes1 :
junior = 150.00
else:
junior1 = int(float(input("How many nights do you intend to stay? ")))
junior2 = (junior*junior1)
if junior1 == 3.0 :
junior_total = (junior*junior1*discount)
junior = float(junior2-junior_total)
junior_fin = format(junior, '.2f')
print("Your total purchase will be",junior_fin,",enjoy your stay at Flappies.")
return
else:
junior = 150.00
junior_total = float(junior*junior1)
junior_fin = format(junior_total, '.2f')
print("Your total purchase will be",junior_fin,",enjoy your stay at Flappies.")
return
main()
我只是不明白为什么它不会工作我很难过,我在空闲时运行它不会显示执行和初级但它会轻松地完成成绩?
if grande == yes or yes1:
被解析为if (grande == yes) or (yes1):
,由于yes1
是非空字符串(在布尔测试中具有真值),因此总是正确的,因此正确的测试将是if grande == yes or grande == yes1:
。
现在有一种更好的方法来处理这种情况:在测试之前规范化用户的输入:
grande = input("Would you like the Grande suite? ")
# .strip() : remove all whitespaces
# .lower() : convert to lowercase
grande = grande.strip().lower()
if grande == yes:
# etc