2014-12-13 80 views
-1

我試圖讓這個程序接受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 () 

回答

1

isValidGuest返回boolean表達式 - 你不應該把它比作在輸入的號碼,只需檢查它是否TrueFalse

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

在你的條件下,你比較客人變量與你測試。 你真正做的是這樣的:

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