簡體   English   中英

為什么我的情況導致我的while循環變為無限?

[英]Why is my condition causing my while loop to become infinite?

我要問用戶的問題是輸入他們的帳單總額,該整數是一個大於零且小於或等於2000的整數。當用戶的輸入不在指定范圍內時,我將繼續要求他們輸入。

之后,我想請用戶按照與上述相同的規則,以大於零且小於或等於20的整數形式輸入晚餐聚會的大小。 如果我可以在程序的第一部分獲得指導,我相信我可以自己完成其余的工作。 這是我到目前為止所擁有的:

bill = int(input('What is the bill total: '))
while bill > 0 and bill <= 2000 :
    bill = int(input('What is the bill total: '))

您的病情逆轉了。 取反:

bill = int(input('What is the bill total: '))
while bill <= 0 or bill > 2000 :
    print "Total must be positive and no more than 2000"
    bill = int(input('What is the bill total: '))

...或者,如果你喜歡“的概念,而輸入是不合法的”,只是把這些東西不在身邊的整個事情:

bill = int(input('What is the bill total: '))
while not (bill > 0 and bill <= 2000) :
    print "Total must be positive and no more than 2000"
    bill = int(input('What is the bill total: '))

如果我了解您想要的內容,那么您希望帳單大於0且小於2000,並且宴會規模必須大於0且小於20,請使用以下方法:

while True:
    bill = int(input('What is the bill total: '))
    while bill > 0 and bill <= 2000:
        size = int(input('What is the party size: '))
        while size > 0 and size <= 20:
            #do stuff
            break
        break
    break

暫無
暫無

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

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