程序不接受任何用户输入

时间:2014-12-13 18:56:32

标签: python validation user-input

我正在尝试让这个程序接受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 ( )

2 个答案:

答案 0 :(得分:1)

isValidGuest会返回一个布尔表达式 - 您不应该将其与输入的数字进行比较,只需检查它是True还是False

while not isValidGuest ( guests ):
    print ("Invalid! Enter only positive whole numbers.")
    guests = input ("Please enter the number of guests: ")

答案 1 :(得分:0)

在您的while条件下,您将guest变量与您的test进行比较。 你真正要做的是:

...
while not isValidGuest(guests):
    ...