简体   繁体   中英

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(...) .

That's why you get TypeError: 'str' object is not callable .

Use other variable for capturing input .

EDIT:

And in your second if statement ( energy <= 0 ) maybe it's worth to do something more than just print notification? Like return ? or at leas include last 4 lines into the else block. But it's just a free thought, maybe you're just getting started with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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