2016-10-25 83 views
0

我問用戶的問題是以大於零且小於或等於2000的整數輸入他們的總賬單。雖然用戶的輸入不在指定的範圍內,但我會繼續詢問他們的輸入。爲什麼我的情況導致我的while循環變得無限?

之後,我想要求用戶按照上面提到的相同規則以大於零且小於或等於20的整數來輸入他們晚餐派對的大小。如果我可以在計劃的第一部分獲得指導,我相信我可以自己完成剩下的工作。這是我到目前爲止:

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

回答

3

你得到了你的情況逆轉。否定它:

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

謝謝表現出很大的 –

+0

你」歡迎。這是一門基礎技術,是我教授課程的早期課程之一。請記住接受答案,即使你必須自己寫。對答案和評論進行投票有助於保持Stack Overflow順利運行。 – Prune

0

如果我明白你想要的,你想要的法案是大於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 
相關問題