我试图让这个程序接受 1 到 1000 之间的数字。我错过了什么?
def main ( ):
getGuestCnt ( )
def getGuestCnt ( ):
guests = input("Please enter the number of guests: ")
while guests != isValidGuest ( guests ):
print ("Invalid! Enter only positive whole numbers.")
guests = input ("Please enter the number of guests: ")
return int(guests)
def isValidGuest ( guests ):
return ((str(guests).isdigit()) and (int(guests) >= 0 and int(guests) <= 1000 ))
main ( )
isValidGuest
返回一个布尔表达式 - 你不应该将其与输入的数字进行比较,只需检查它是True
还是False
:
while not isValidGuest ( guests ):
print ("Invalid! Enter only positive whole numbers.")
guests = input ("Please enter the number of guests: ")
在您的 while 条件下,您将客人变量与测试进行比较。你真正要做的是:
...
while not isValidGuest(guests):
...