我问用户的问题是将他们的总账单输入为大于零且小于或等于2000的整数。当用户的输入不在指定范围内时,我将继续询问他们输入。
在此之后,我想要求用户按照与上述相同的规则输入他们的晚宴的大小,作为大于零且小于或等于20的整数。如果我可以对我的课程的第一部分有所指导,我相信我可以自己完成剩下的工作。这就是我到目前为止:
bill = int(input('What is the bill total: '))
while bill > 0 and bill <= 2000 :
bill = int(input('What is the bill total: '))
答案 0 :(得分: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: '))
答案 1 :(得分: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