簡體   English   中英

在此 while 循環中,如何不接受除“是”或“否”以外的任何字符串值

[英]How do I accept no string values other than 'Yes' or 'No' in this while loop

我對編程很陌生,所以請原諒這個愚蠢的問題。 我如何進行這個循環,以便如果該人輸入一個隨機字符串(不是是或否),它將返回“無效響應”?

name = input("What's your name? ")
understanding = input("{}, do you understand while loops in python\n(Yes or No)? ".format(name))


while understanding.lower() != 'yes':
    print("Ok, {}, while loops in Python repeat as long as a certain Boolean condition is met.".format(name))
    understanding = input("{}, now do you understand Python while loops?\n(Yes or No)? ".format(name))
print("Great news {}! It's good to hear that you understand these loops now! Let's move on!".format(name))

也許像這樣

if understanding.lower() not in ('yes','no'):
    print('Invalid response')

暫無
暫無

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

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