Can';t fix“;类型错误:';str';对象不可调用";用Python
我需要帮助修复错误。这是我的密码:Can';t fix“;类型错误:';str';对象不可调用";用Python,python,random,Python,Random,我需要帮助修复错误。这是我的密码: import random def game(): capitals={"England":"London","France":"Paris","Belgiom":"Brussels",\ "Canada":"Ottawa","China":"Beijing","Cyprus":"Nicosia",\ "Cuba":"Havana","Egypt":"Cairo","Gre
import random
def game():
capitals={"England":"London","France":"Paris","Belgiom":"Brussels",\
"Canada":"Ottawa","China":"Beijing","Cyprus":"Nicosia",\
"Cuba":"Havana","Egypt":"Cairo","Greece":"Athens",\
"Ireland":"Dublin","Italy":"Rome","a":"A","B":"B"}
wrong=[]
right=[]
incorrect_answers = False
while len(capitals)>0:
pick = random.choice(list(capitals.keys()))
correct_answer = capitals.get(pick)
print ("What is the capital city of" + pick + "?")
answer = input("Your answer: ")
if answer.lower() == correct_answer.lower():
print ("That's Correct!\n")
del capitals[pick]
right.append(pick)
else:
print ("That's Incorrect.\n")
print ("The correct answer is" + correct_answer + "\n")
wrong.append(pick)
incorrect_answers = True
del capitals[pick]
print ("You got ",len(right), "/", len(wrong))
top = len(right)
bottom = len(wrong)
perc = float((top / bottom) * 100)
print(perc)
if incorrect_answers:
print ("Here are the ones that you may want to brush up on:\n")
for each in wrong:
print (each)
else:
print ("Perfect!")
def help():
print("do you neeeded efhdufghaf dfgjn")
while True:
input = input("what do you want to do? help or play?")
if input == "help":
help()
break
if input == "play":
print("you want to play")
game()
break
你不应该这样做
input = input("what do you want to do? help or play?")
您正在使用变量隐藏函数
输入
。将变量的名称更改为其他名称。提供回溯,以便我们知道错误所在。回溯之间的行(最近一次调用):
到类型错误:错误消息中显示的“str”对象不可调用
在调试时是一个真正的金矿,指出代码的哪一部分导致了问题等。你应该确保在几乎每个问题的结尾都有这样的问题。我现在已经解决了。不过还是谢谢你。这是我的第一篇帖子,所以我不知道所有的规则…这很有效。我是Python新手,所以感谢您指出这一点。