我的错误是:
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 :(得分:5)
您已将名称range
重命名为变量名称。别这么做。