TypeError:'int'对象不可调用Python 3
问题描述:
好的,所以,我试图做一个“猜数字”游戏,一个游戏,你说一个数字,另一个玩家说“下”或“更高“取决于你的答案,并且当你正确地猜出你的号码时,你就赢了。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()
答
....
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.
....
所以,请改变你的函数名或变量名。
答
变量名称与函数名称相同。您忘记了在正确的猜测字符串处添加“尝试”。
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
请改变你的函数名...... –
您有一个名为'guess'功能,以及一个名为变量'guess' ......“猜测”可能会发生什么.. – donkopotamus