回到python的前一行?

时间:2013-10-28 21:22:08

标签: python

我试图创建以下内容,以便在我去的时候重新分配变量。

def train(old_strength, old_energy): 
    train = input("What would you like to do now?\n'strentgh' or 'rest'\n>")

    if train == 'strength':
        strength = old_strength+10
        energy = old_energy-10


        if energy <= 0:
            print ("You do not have enough energy to do this.")

        else:   
            pass

        print ("You have gained 10 strength, and now have %d strength and %d energy left" %(strength, energy))
        old_strength = strength)
        old_energy = energy)        
        train(old_strength, old_energy)

我试图得到它,以便当它返回到“火车”输入时,它将在下一次使用新的力量和能量值。 目前我收到了错误

Traceback (most recent call last):
  File "settler.py", line 127, in <modul
    train(10, 50)
  File "settler.py", line 37, in train
    train(old_strength, old_energy)
TypeError: 'str' object is not callable

我该如何解决这个问题? 非常感谢提前

1 个答案:

答案 0 :(得分:4)

您通过撰写def train(....)str重新签名train = input(...)

这就是你得到TypeError: 'str' object is not callable的原因。

使用其他变量捕获input

修改

在您的第二个if声明(energy <= 0)中,除了打印通知之外,还有什么值得做的事情?像return一样?或者在leas中包含else块中的最后4行。但这只是一个自由思想,也许你刚刚开始这个。