【问题标题】:if statement, going back and forthif 语句,来回
【发布时间】:2022-01-04 05:07:23
【问题描述】:

我是 python 新手,但编写了一些结构化文本和一点点 c++ 现在我正在使用 if 语句解决一个问题,它似乎不像我习惯的那样工作

示例代码:

more_guests = 0
loop = bool; loop = False
ferdig = int; ferdig = 1
while loop == False:
    guests = []
    more_guests = 0
    if int(ferdig) == 1:
        guest = input("type in guest ")
        more_guests = int(input("done? 1 for yes 2 for no "))
        if int(more_guests) == 1:
          guests.append(guest)
          ferdig == 3
        elif int(more_guests) == 2:
            guests.append(guest)
            ferdig == 2
        else:
            print("unvalid answer, please use 1 or 2")
            ferdig == 1
    elif int(ferdig) == 2:
        ferdig = 1
    elif int(ferdig) == 3:
        print(guests)
    else:
        loop = True

我试过确保它是一个整数等等,只是一直卡在完成? 1 表示是 2 表示否,它总是让我循环输入客人

在使用结构化文本时,我经常使用这种来回切换的方式,但我似乎无法在 python 中理解它,也许我应该使用 case/switch? 无论如何,如果有人可以帮助我理解您是否可以在 python 中以这种方式使用 IF 语句,我们将不胜感激

【问题讨论】:

    标签: python if-statement switch-statement


    【解决方案1】:

    这是另一种使用函数来完成您尝试做的事情的选项。

    guests = []
    
    def add_guest():
    
        guest = input("type in guest ")
        guests.append(guest)
        add_more()
    
    def add_more():
    
        more_guests = int(input("done? 1 for yes 2 for no ")) 
    
        #if they answer 1 we print out the final list
        if more_guests == 1: 
            print("the guest list is {}".format(guests))
            print('have a nice day')  
    
        #if they answer 2 we go to the add_guest function    
        elif more_guests == 2:  
            add_guest()
    
        #if they answer anything other than 1 or 2 we re-call the add_more function.  
        else: 
            print("unvalid answer, please use 1 or 2") 
            add_more()
    
    add_guest()
    

    【讨论】:

    • 有趣,在循环中使用函数有什么好处?
    • 就我个人而言,我尽量避免使用 While 循环,因为如果你不小心它们会导致无限循环。我通常还发现函数更容易阅读和理解。但这真的取决于你和你喜欢什么。
    【解决方案2】:

    我认为你写错了运算符。 ferdig == 1 只是比较值运算符。所以它返回 true(1) 或 false(0)。

    如果你想改变 ferdig 的值,你应该在 if 语句中写 ferdig = 3。

    【讨论】:

    • 哈哈,那是我的错,谢谢伙计!
    【解决方案3】:

    我已经对你的代码进行了一些清理,以获得我认为你想要的东西。

    guests = []
    while True:
        guest = input("type in guest ")
        guests.append(guest)
        response = int(input("done? 1 for yes 2 for no "))
        if response == 1:
            print(guests)
            break
        elif response == 2:
            continue
        elif response == 3:
            print(guests)
        else:
            print("invalid answer, please use 1 or 2")
            
    

    【讨论】:

    • 是的,谢谢,很快就注意到这是一团糟,必须一次做一件事才能让我专注于工作;)非常感谢!
    【解决方案4】:

    您的代码有很多问题,这里已经修复:

    more_guests = 0
    # you don't need to declare types, but if you want to, this is how
    loop: bool = False
    # however, Python can just infer the type itself like this
    ferdig = 1
    # you don't want to reset guests every time around the loop
    guests = []
    while loop == False:
        more_guests = 0
        if int(ferdig) == 1:
            guest = input("type in guest ")
            # you always want to append a guest, including if someone types a 1 after
            guests.append(guest)
            more_guests = int(input("done? 1 for yes 2 for no "))
            if int(more_guests) == 1:
                ferdig = 3
            elif int(more_guests) == 2:
                ferdig = 2
            else:
                print("invalid answer, please use 1 or 2")
                ferdig = 1
        elif int(ferdig) == 2:
            ferdig = 1
        elif int(ferdig) == 3:
            # after printing, you're done, you don't want to print forever
            print(guests)
            loop = True
    

    请注意,尽管您的大部分代码并不是真正需要的,但您正在做 Python 可以为您做的大量簿记工作,或者根本不需要:

    # this isn't needed, because you set that at the start of the loop anyway
    # more_guests = 0
    # this isn't needed, because you can tell when to stop from ferdig
    # loop: bool = False
    # starting at 2, since that means you want to keep going
    ferdig = 2
    guests = []
    while ferdig != 1:
        # this isn't needed, you can just read ferdig
        # more_guests = 0
        # this isn't needed, you want a new guest on every loop
        #if int(ferdig) == 1:
        guest = input("type in guest ")
        guests.append(guest)
        ferdig = int(input("done? 1 for yes 2 for no "))
        # none of this is needed, all you need to know is if ferdig is 1 or 2
        # if int(more_guests) == 1:
        #     ferdig = 3
        # elif int(more_guests) == 2:
        #     ferdig = 2
        # else:
        if ferdig not in (1, 2):
            print("invalid answer, please use 1 or 2")
            ferdig = 1
        # this is also not needed, at this point ferdig will be 1 or 2
        # elif int(ferdig) == 2:
        #     ferdig = 1
        # elif int(ferdig) == 3:
    # put the print outside the loop and it only prints once
    print(guests)
    # got rid of this
    # loop = True
    

    所以,这只是:

    ferdig = 2
    guests = []
    while ferdig != 1:
        guest = input("type in guest ")
        guests.append(guest)
        ferdig = int(input("done? 1 for yes 2 for no "))
        if ferdig not in (1, 2):
            print("invalid answer, please use 1 or 2")
            ferdig = 1
    print(guests)
    

    最后,这会做同样的事情:

    more_guests = True
    guests = []
    while more_guests:
        guests.append(input("type in guest "))
        more_guests = input("done? 1 for yes") != '1'
    print(guests)
    

    【讨论】:

    • 谢谢,注意到我继续的时候速度很快,但是已经清理了我的,所以它可以工作,虽然没有这里那么好!只是在尝试前进,因为我对 python 还不是很熟悉,而且我已经有一段时间没有编程了,但是非常感谢这个很好的例子和解释!
    • 我很好奇这条线虽然more_guests = input("done? 1 for yes") != '1' 当循环在 more_guests 上运行时输入 1 时它为什么会停止 = True 之前没有看到它使用过,所以只是挣扎了一下
    • 没关系,有点密集。 input("done? 1 for yes") 只是获取输入,作为一个字符串。 x != '1' 是一个表达式 True 如果 x 不是 '1'False 否则 - != 表示“不等于”。所以,more_guests = input("done? 1 for yes") != '1' 意味着more_guests 应该是True,如果用户在该提示符上输入的某些文本不是'1',这正是您需要的:输入'1',more_guests 将是@987654338 @,否则为True
    • 我想一般情况下可能会非常令人困惑的是:x = y == 2 - 单个等号是一个赋值,因此 Python 在某种意义上将其读取为 x = (y == 2)。双等号是一种比较,这将产生一个布尔值TrueFalse,这取决于双方是否具有相同的值(或者为被比较的类型定义了相等性)。所以它适用于x = Truex = False
    • 是的,好吧,我自己认识它们,只是在组合时没有看到整个画面,所以如果我理解正确,它会在你输入 1 时变为more_guests = (1 != 1),然后变为more_guests = (False)感谢一堆实际上帮助很大的人!
    猜你喜欢
    • 2015-07-28
    • 2018-05-07
    • 2021-08-31
    • 2020-07-30
    • 1970-01-01
    • 2023-02-21
    • 2013-09-09
    • 2018-04-18
    • 2020-09-12
    相关资源
    最近更新 更多