Home > Net >  Endlessly looping the same function after inputting action once
Endlessly looping the same function after inputting action once

Time:04-27

I've been making a little game, but when i choose and action such as "attack", it keeps looping the same function over and over.

I can never choose another action like "inventory":

def attack(): 
    # this has way more code but not important to demo the problem
    # see the actual output below for what happes when this is called
    print("Attacking.")

def flee(): 
    print("Fled to safety.")

def inventory(): 
    print("Backpack is empty.")

def shop(): 
    print("No shop near-ish. Got no money")


action = input('Which action would you like to choose? ')

while action != "Exit Game":
    if action == "attack":
        attack()
    elif action == "flee":
        flee()
    elif action == "inventory":
        inventory()
    elif action == "shop":
        shop()
    else:
        break

Example of the simulated (attack) outcome - I only get to choose the action onces and then it repeats endlessly:

Which action would you like to choose? attack
Attacking.
Attacking.
Attacking.
Attacking.
Attacking.
Attacking.
Attacking.
# etc

Example of the real (attack) code outcome:

This is an example of the outcome

CodePudding user response:

You need to update the user's desired action for every iteration of the loop. Right now you only ask once and never again. Instead, try this

while True:
    action = input('Which action would you like to choose?')
    if ...

CodePudding user response:

You have to read action at the beginning of your decision tree

And - this code worked for me only after several changes (identation, global vars, missing var

import random



def attack():

   global HP
   global EnemyHP
   global Coins
   coin_multiplyer=2
   
   print('You try to strike the enemy')
   if random.randint(0, 10) >= 4:
       print('You missed the enemy')
       print('The enemy attacks you')
       HP = HP - 4
       print('You have '   str(HP)   'HP left')

   else:
       if random.randint(0, 10) >= 3:
           print('Critical Hit!')
           EnemyHP = EnemyHP - 7
       else:
           print('You hit the enemy')
           EnemyHP = EnemyHP - 4
       print('The enemy has '   str(EnemyHP)   'HP left')
       print('You have '   str(HP)   'HP left')

   if EnemyHP <= 0:
       print('You killed the goblin!')
       print('Oh, the goblin left some golden coins!')
       print('You recived 12 golden coins!')
       Coins = Coins   12 * coin_multiplyer
       print('You have '   str(Coins)   'gold coins')
       print('Oh no! Another goblin appeared!')
       EnemyHP = 10

#Below here is the code that summons the function depending on what action you choose


HP=0
EnemyHP=0
Coins=0

while action != "Exit Game":
   action = input('Which action would you like to choose?')
   if action == "attack":
       attack()
   elif action == "flee":
       flee()
   elif action == "inventory":
       inventory()
   elif action == "shop":
       shop()
   else:
       break

CodePudding user response:

You need to have your input call inside the loop:

while True:
    action = input('Which action would you like to choose?')
    if action == "attack":
    ...
    else:
        break

Also, an approach I usually take for a code like yours is to implement a switch dictionary, so that you don't end up with a lot of if ... else:

actions = {
    "attack": attack,
    "flee": flee,
    "inventory": inventory,
    "shop": shop,
    "Exit Game": lambda: True,
}

while True:
    try:
        if actions[input('Which action would you like to choose?')]():
            break
    except KeyError:
        pass
  • Related