简体   繁体   English

返回python的前一行?

[英]Go back to previous line in python?

Im trying to create the following such that it reassigns the variable as I go. 我试图创建以下内容,以便在我进行操作时重新分配变量。

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)

Im trying to get it so that when it goes back to the 'train' input, it will then use the new strength and energy values for the next time. 我试图获取它,以便当它返回到“火车”输入时,它将在下一次使用新的强度和能量值。 At the moment I get the error 此刻我得到了错误

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

How do I fix this? 我该如何解决? Many Thanks in advance 提前谢谢了

You reasigned def train(....) with str by writing train = input(...) . 您通过写train = input(...)重新分配了带strdef train(....) train = input(...)

That's why you get TypeError: 'str' object is not callable . 这就是为什么出现TypeError: 'str' object is not callable

Use other variable for capturing input . 使用其他变量捕获input

EDIT: 编辑:

And in your second if statement ( energy <= 0 ) maybe it's worth to do something more than just print notification? 在您的第二条if语句( energy <= 0 )中,也许值得做的不仅仅是打印通知? Like return ? 喜欢return吗? or at leas include last 4 lines into the else block. 或者至少将最后4行包含在else块中。 But it's just a free thought, maybe you're just getting started with this. 但这只是一个自由的想法,也许您才刚刚开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM