TypeError:' str' if语句不能调用object

时间:2017-04-07 18:48:18

标签: python python-3.x

我已经阅读了50多个标题相似的帖子,还没有回答我的问题。我打电话回到开始有什么问题?我使用的是Python 2.7.7

def Skill():

    global PSkill
    global Fee

    Skill = input ("Please enter your skill level - you must either enter <C> for casual or <E> for expert!!")

    if Skill == "C" or Skill == "c":
        PSkill = "Casual"
        Fee = 30

    elif Skill == "E" or Skill == "e":
        PSkill = "Expert"
        Fee = 45

    else:
        print("Sorry I don't recognise that answer - please enter the answer again")
        Skill()

进一步向下,我有这段代码,当它们是早先的错误时,它们不会工作。

TotalFee = float(Rate) * float(Fee)
TotalFee = str(TotalFee)

我得到的错误是......

Traceback (most recent call last):
line 106, in <module>
Main()
line 93, in Main
Main()
line 68, in Main
Currency()
TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:3)

您已在功能技能中重新定义了技能符号。更改其中一个的名称。例如:

level = input ("Please enter your skill level - you must either enter <C> for casual or <E> for expert!!")

if level == "C" or level == "c":
    PSkill = "Casual"
    Fee = 30

elif level == "E" or level == "e":
    PSkill = "Expert"
    Fee = 45

现在,当您尝试重复技能时,该名称仍然引用该函数,而不是局部变量。

其他问题:

您应该使用while循环来获取输入,直到获得有效输入。递归会向运行时堆栈添加垃圾。

level = ""
while level not in "CcEe":
    Skill = input ("Please enter your skill level - you must either enter <C> for casual or <E> for expert!!")