Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在Python2.7中有一个错误,不知道如何修复它_Python_Typeerror - Fatal编程技术网

我在Python2.7中有一个错误,不知道如何修复它

我在Python2.7中有一个错误,不知道如何修复它,python,typeerror,Python,Typeerror,我的错误是: 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'

我的错误是:

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     
文件“gussing_game.py”,第117行,在
游戏()
游戏中第78行的文件“gussing_game.py”
对于范围内的猜测(0,猜测的数量):
TypeError:“int”对象不可调用
我的代码:(顺便说一句,在我的计算机上,函数中的所有内容都有一个额外的缩进,我的代码刚刚复制粘贴错误)

导入时间
#它会清除您留在终端中的所有代码
导入操作系统
操作系统(“清除”)
def game():
#导入随机模块,以便它可以创建随机数
随机输入
最小所需数量=100
打印“您想猜测的数字范围(请记住,您至少需要100个数字才能猜测,最多只能猜测20个)”
范围=输入()
范围=整数(范围)
所需数量=整数(范围)
当所需数量<最小所需数量时:
打印“您所需的猜数从低到低”
打印“请选择另一个大于100的数字”
范围=输入()
范围=整数(范围)
所需数量=整数(范围)
如果所需数量>最小所需数量:
#生成随机数并存储它
answer=random.randint(0,所需数量)
#设置猜测的数量
最大猜测=20
#将胜利者设为假
获胜者=0
打印“游戏的目的是猜测1到%s之间的数字。您希望进行多少次猜测?(最多允许20次猜测)”%(所需数字)
数字=输入()
数字=整数(数字)
猜数=int(数字)
当猜数>最大猜数时:
打印“您想要的猜测次数太多”
打印“请选择另一个20或更少的号码”
数字=输入()
数字=整数(数字)
猜数=int(数字)
如果猜数小于最大猜数:
#打印完成的说明
打印“您现在只有%s次猜测来猜测您的数字”%(猜测次数)
#开始左尝试次数循环
猜数=int(数字)
对于范围内的I(0,猜测次数):
#询问电话号码
打印“请输入您的猜测”
#接收数字并说出数字是高还是低
猜测=输入()
#这会将猜测从文本转换为整数,并再次存储它
猜测=int(猜测)
如果猜测>回答:
打印“您的号码过高”
elif guess<答案:
打印“您的答案太低”
elif guess==答案:
打印“耶,你找到答案了”
获胜者=1
打破
#若数字正确,则停止循环
#说号码是
如果获胜者==0:
打印“你已经使用了你所有的猜测”
打印“号码为%s%”(答案)
打印“您想再次播放吗?(0/1)”
redo=int(输入())
如果重做==1:
游戏()
操作系统(“清除”)
elif redo==0:
打印“好的,再见!!!”
时间。睡眠(3)
操作系统(“清除”)

game()

您已将名称
范围
用作变量名来覆盖它。不要这样做。

你需要修正缩进。不要随意输入文本来过滤。它的存在是有原因的;如果它抱怨,那是因为你没有对你的问题做出足够的解释。正如@DanielRoseman指出的,你已经重写了你的range函数,但是如果你使用的是2.7,你应该使用xrange()。它在3.3中的和范围被更改为与xrange()相同。当你是说我已经重写了范围函数时,你是说这已经是python中的一个命令了,它只是对它进行了重新编程,所以我只需要更改变量(对不起,我是python的新手)@BobTrumen它“已经是python中的一个命令”并不重要-如果您定义了自己的函数,然后通过为相同的名称分配其他名称来对其进行阴影处理,则会出现相同的问题。使用
range
,只要不尝试调用该函数就可以了——Python中的名称一次只能引用一个对象。
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")