2015-10-07 50 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

相关问题