【发布时间】:2015-01-08 13:35:33
【问题描述】:
我需要帮助来修复错误。这是我的代码:
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
【问题讨论】:
-
提供回溯,以便我们知道错误在哪里。
-
Traceback (most recent call last):到TypeError: 'str' object is not callable之间出现在错误消息中的行是调试时名副其实的金矿,指出代码的哪个区域导致问题等。你应该确保你有几乎所有问题的结尾都是这样。 -
我现在已经修好了。不过还是谢谢。这是我的第一篇文章,所以不知道所有规则...