2015-11-08 81 views
0

好的,所以,我試圖做一個「猜數字」遊戲,一個遊戲,你說一個數字,另一個玩家說「下」或「更高「取決於你的答案,並且當你正確地猜出你的號碼時,你就贏了。TypeError:'int'對象不可調用Python 3

也許這已經回答了,但我無法弄清楚什麼是錯的。

我不明白,如果在你自己調用的函數內,它應該再次運行自己,對吧?

不知道是否有幫助,但我使用Python 3

number = 897 
attempts = 0 

def guess(): 
    guess = input("Number: ") 
    guess = int(guess) 
    global attempts 
    if guess > number: 
     print("It's lower.") 
     attempts = attempts + 1 
     guess() 
    elif guess < number: 
     print("It's higher.") 
     attempts = attempts + 1 
     guess() 
    else: 
     print("Correct! The number was " + str(number) + "!") 
     print("It took you " + str(attempts) + "!"), 

print("I'm thinking of a number, guess it!") 
guess() 
+1

請改變你的函數名...... –

+5

您有一個名爲'guess'功能,以及一個名爲變量'guess' ......「猜測」可能會發生什麼.. – donkopotamus

回答

0
.... 

def guess():     # guess is a function 
    guess = input("Number: ") 
    guess = int(guess)   # it's become int now 
    global attempts 
    if guess > number: 
     print("It's lower.") 
     attempts = attempts + 1 
     guess()     # you're trying to call an int object, because you defined it as a int object as I said. 

.... 

所以,請改變你的函數名或變量名。

0

變量名稱與函數名稱相同。您忘記了在正確的猜測字符串處添加「嘗試」。

number = 897 
attempts = 0 

def guess1(): 
    guess = input("Number: ") 
    guess = int(guess) 
    global attempts 
    if guess > number: 
     print("It's lower.") 
     attempts = attempts + 1 
     guess1() 
    elif guess < number: 
     print("It's higher.") 
     attempts = attempts + 1 
     guess1() 
    else: 
     print("Correct! The number was " + str(number) + "!") 
     print("It took you " + str(attempts) + " attempts !"), 

print("I'm thinking of a number, guess it!") 
guess1() 
+0

「你忘了添加「嘗試」正確的猜測字符串。「我意識到在發佈這裏的代碼後xd – zCraazy