2015-10-07 45 views
-4

我的錯誤是:我在Python 2.7的錯誤和不知道如何解決它

File "guessing_game.py", line 117, in <module>    
    game()        
    File "guessing_game.py", line 78, in game    
    for guesses in range (0,NUMBER_OF_GUESSES):   
TypeError: 'int' object is not callable  

我的代碼:(順便說一句我的電腦上的一切功能有一個額外的縮進我的代碼只是複製和粘貼錯)

import time 
#It clears all the code you have left in the terminal 
import os 
os.system("clear") 

def game(): 



#Import the random module so it can create the random number 
import random 
MIN_DESIRED_NUMBER = 100 
print "what range of numbers do you want to guess in (keep in mind you need a minimum of 100 numbers to guess from and you only get a maximum of 20 guesses)" 
range = input() 
range = int(range) 
DESIRED_NUMBER = int(range) 
while DESIRED_NUMBER < MIN_DESIRED_NUMBER: 
    print "Your desired number to guess from is to low" 
    print "Please choose another Number that is bigger 100 or bigger" 
    range = input() 
    range = int(range) 
    DESIRED_NUMBER = int(range) 

if DESIRED_NUMBER > MIN_DESIRED_NUMBER: 

#Generate the random number and store it 
    answer = random.randint(0,DESIRED_NUMBER) 


#Set number of guess's 





MAX_GUESS = 20 


#set winner to false 
winner = 0 


print "The aim of the game is to guess a number between 1 and %s. How many guess would you like to have? (you are only allowed a maximum of 20 guesses)" % (DESIRED_NUMBER) 
number = input() 
number = int(number) 
NUMBER_OF_GUESSES = int(number) 
while NUMBER_OF_GUESSES > MAX_GUESS: 
    print "Your desired number of guesses is to high" 
    print "Please choose another Number that is 20 or less" 
    number = input() 
    number = int(number) 
    NUMBER_OF_GUESSES = int(number) 

if NUMBER_OF_GUESSES < MAX_GUESS: 



#Print Completed instructions 

     print "You will now only have %s guesses to guess your number" % (NUMBER_OF_GUESSES) 


    #Start the number loop of tries left 
NUMBER_OF_GUESSES = int(number) 
for I in range (0,NUMBER_OF_GUESSES): 
    #Ask for number 
    print "Please enter your guess" 

    #Recive number and say if the number is hight or lower 

    guess = input() 
    #This converts guess from the text into an integer and the stores it again 
    guess = int(guess) 
    if guess > answer: 
     print "Your number is to high" 
    elif guess < answer:  
     print "Your answer is to low" 
    elif guess == answer: 
     print "YAY YOU GOT THE ANSWER" 
     winner = 1 
     break 


    #Stop loop if number is correct 


#Say that the number was 
if winner == 0: 
    print "You have used all of your guesses" 
print "The number was %s" % (answer) 


print "would you like to play again? (0/1)" 
redo = int(input()) 
if redo == 1: 
    game() 
    os.system("clear") 
elif redo==0: 
    print "alright bye!!!" 
time.sleep(3) 
os.system("clear") 

遊戲()

+0

您需要修復縮進。 –

+1

不要隨機輸入文字來擊敗過濾器。這是有原因的;如果它在抱怨,那是因爲你沒有對你的問題提供足夠的解釋。 –

+0

正如@DanielRoseman指出你已經覆蓋了你的範圍函數,但是如果你使用2.7,你應該使用xrange()。它[更好](3.3)範圍內更改爲(http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange-functions-in-python-2-x)與xrange()相同。 – SirParselot

回答

5

你使用它作爲一個變量名覆蓋名range。不要這樣做。

+0

當你的意思是我已經覆蓋了範圍函數你的意思是說,這已經是一個命令在Python中,它只是劑量識別它,所以我只需要改變變量(抱歉,即時通訊新的Python) –

+1

@BobTrumen它並不真正這是*「已經是Python中的命令了」* - 如果您定義了自己的函數,則會出現同樣的問題,然後通過將其他名稱分配給相同的名稱來映射它。只要你沒有嘗試調用這個函數,使用'range'就沒問題了。 - Python中的名字一次只能引用一個對象。 – jonrsharpe

相關問題