簡體   English   中英

嘗試在 python 中使用具有多個條件的 while 循環

[英]Trying to use while loop with multiple conditions in python

guests0, guests1 = ['Jane','Katy', 'Michelle'], ['John','Keth','Mike']

while guests0 != [] and guests1 != []:
    doorkeeper = print('Who is there?')
    name = input().capitalize()
    if name in guests0:
        guests0.remove(name)
        print('Welcome, ', name)
    elif name in guests1:
        guests1.remove(name)
        print('Welcome, ', name)
    else:
        print('Sorry, we are closed')
    print(guests0)
    print(guests1)

我希望循環繼續下去,直到兩個列表中都沒有更多的名字。 但是循環會繼續,直到只滿足一個條件。 我不知道到底出了什么問題,我該如何解決。 而且我只想使用非常簡單、基本的方法來解決這個問題。

正如有人提到的,你應該使用 an or not and

為什么? 使用真值表進行評估:

guests0 != [] guests1 != [] 或者
真的 真的 真的 真的
真的 錯誤的 錯誤的 真的

在第一行你可以看到當我們開始時兩者都不為空。 然后在第二行,當我們清空一個列表時,使用時比較結果為False and因為一個列表是空的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM