简体   繁体   English

为什么我的程序在外壳中没有显示嵌套的if和else?

[英]why isn't my program not showing nested if and else in the shell?

Still can't figure why Grande will run through completely through but executive and junior won't? 仍然不知道为什么格兰德会完全通过,而高管和初级不会呢? I've tried removing the return but then that causes the if statements to run into each other. 我试过删除返回值,但是这会使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()

I just don't understand why it won't work I'm stumped, I run it in idle it won't display executive and junior but it will run through grade with ease? 我只是不明白为什么它不起作用我感到困惑,我在空闲状态下运行它,不会表现出高管和初级,但会轻松地升入年级?

if grande == yes or yes1: is parsed as if (grande == yes) or (yes1): , which is always true since yes1 is a non-empty string (which has a true value in a boolean test), so the correct test would be if grande == yes or grande == yes1: . if grande == yes or yes1:被解析为if (grande == yes) or (yes1):则始终为true,因为yes1是一个非空字符串(在布尔测试中具有真值),因此正确的测试将是if grande == yes or grande == yes1:

Now there's a much better way to handle this situation: normalize the user's input before the test: 现在,有一种更好的方法来处理这种情况:在测试之前将用户的输入标准化:

grande = input("Would you like the Grande suite? ")
# .strip() : remove all whitespaces
# .lower() : convert to lowercase
grande = grande.strip().lower()

if grande == yes:
   # etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM