【问题标题】:Can't fix "TypeError: 'str' object is not callable" in Python无法在 Python 中修复“TypeError:'str' 对象不可调用”
【发布时间】: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 之间出现在错误消息中的行是调试时名副其实的金矿,指出代码的哪个区域导致问题等。你应该确保你有几乎所有问题的结尾都是这样。
  • 我现在已经修好了。不过还是谢谢。这是我的第一篇文章,所以不知道所有规则...

标签: python random


【解决方案1】:

你不应该这样做

input = input("what do you want to do? help or play?")

您正在使用您的变量隐藏函数input。将变量的名称更改为其他名称。

【讨论】:

  • 这行得通。我是 Python 新手,所以感谢您指出这一点。
猜你喜欢
  • 2020-05-16
  • 2015-02-15
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2012-06-04
  • 1970-01-01
相关资源
最近更新 更多