Python Beginner Project- Text-Based RPG

Hello,

I am having issues with my first personal project. I wanted to put into practice basic things I learned after completing the Python 3 course. What I am mostly looking for regards my battle() function:

  1. Why does the attack() function accept all inputs, instead of just the selected choices I wrote?
  2. Why does the enemy_health variable increase and decrease randomly, instead of decreasing to 0?

Any guidance would be appreciated! Still a work in progress.

P.S.
I understand that classes and OOP would fit this perfectly but I wanted to practice something lower-level, on my own, before tackling that difficult topic.

#BLACK MAGIC ACROPOLIS

#Text-Based Adventure Game
#by c.l90
#------------------------------------------------------------#



import random
import math 

def scenario_selection():
    scenario_list = ['Dark Temple', 'Occultist\'s Library', 'Sorcerer\'s Trial']
    random_scenario = random.choice(scenario_list)
    def selection_process():
        scenario_choice = input('\nChoose a scenario:\n- Dark Temple (D)\n- Occultist\'s Library (O)\n- Sorcerer\'s Trial (S)\n- Random (R)\n>> ').upper()
        if scenario_choice == 'D':
            print('You have chosen the Dark Temple episode')
        elif scenario_choice == 'O':
            print('You have chosen the Occultist\'s Library episode')
        elif scenario_choice == 'S':
            print('You have chosen the Sorcerer\'s Trial episode')
        elif scenario_choice == 'R':
            print(f'You are playing the {random_scenario} episode')
        else:
            print('Invalid selection. Please try again.')
            selection_process()
    selection_process()

def character_selection():
    character_list = ['Occultist', 'Hunter', 'Emissary']
    random_choice = random.choice(character_list)
    def choice_process():
        player_choice = input('\nSelect your character:\n- Occultist (O)\n- Hunter (H)\n- Emissary (E)\n- Random (R)\n>> ').upper()
        if player_choice == 'O':
            print('You have chosen the role of the Occultist')
        elif player_choice == 'H':
            print('You have chosen the role of the Hunter')
        elif player_choice == 'E':
            print('You have chosen the role of the Emissary')
        elif player_choice == 'R':
            print(f'You are the {random_choice}')
        else:
            print('Invalid selection. Please try again.')
            choice_process()
    choice_process()



def battle():

    #Setup
    enemy_list = ['Prophet', 'Mercenary', 'Practitioner', 'Demon', 'Foot Soldier', 'Wild Boar']
    random_enemy = random.choice(enemy_list)
    print(f'You have encountered the {random_enemy}.\n')
    global enemy_health
    enemy_health = 100


    #Attack Sequence Function
    def attack():
        attack_damage = random.randint(1, 100)
        initial_player_choice = input('Choose an attack:\n- Spirit Ambush (S)\n- Dark Incantation (D)\n- Morningstar Attack (M)\n- Poisoned Arrow (P)\n>> ').upper()
        if initial_player_choice == 'S' or 'D' or 'M' or 'P':
            enemy_health -= attack_damage
            print(f"Attack successful. Enemy health is now at {enemy_health}")
        else:
            print('Invalid choice.')

    while enemy_health > 0:
        attack()
    print('You have successfully defeated the enemy.')





def gameplay():
    scenario_selection()
    print('\n')
    character_selection()
    print('\n')
    battle()

battle()


Hi,
You can’t chain the conditions like you have. They need to be fully described, as each is resolved independently of the others.
e.g.
if a == b or a == c or a == d:

So, the second condition in your if statement could basically be written as;
if ‘D’:
and would evaluate as true as it has a value (as would ‘M’ and ‘P’).

Thank you, I have done as you mentioned. I cannot grasp why, after the user attacks, the enemy’s health fluctuates from up to down. I had written enemy_health -= attack_damage, but it still fluctuates after printing. I realize that this may be because each time enemy_health updates, it may have updated with a larger number from random.randint(). Any ideas on how to make it only decrease each time?

Updated code:

def battle():

    #Setup
    enemy_list = ['Prophet', 'Mercenary', 'Practitioner', 'Demon', 'Foot Soldier', 'Wild Boar']
    random_enemy = random.choice(enemy_list)
    print(f'You have encountered the {random_enemy}.\n')
    


    #Attack Sequence Function
    def attack():
        enemy_health = 100
        attack_damage = random.randint(1, 100)

        while enemy_health > 0:
            initial_player_choice = input('Choose an attack:\n- Spirit Ambush (S)\n- Dark Incantation (D)\n- Morningstar Attack (M)\n- Poisoned Arrow (P)\n>> ').upper()
            if initial_player_choice == 'S' or initial_player_choice == 'D' or initial_player_choice == 'M' or initial_player_choice == 'P':
                enemy_health -= attack_damage
                print(f"Attack successful. Enemy health is now at {enemy_health}")
                attack()
            else:
                print('Invalid choice.')
                attack()
        print('You have successfully defeated the enemy.')

    attack()```
import random
enemy_health = 100
def battle():

    #Setup
    enemy_list = ['Prophet', 'Mercenary', 'Practitioner', 'Demon', 'Foot Soldier', 'Wild Boar']
    random_enemy = random.choice(enemy_list)
    #just set the enemy health here so that it would remain at 100 when the game starts and gets reduced when attacked wihtout again going to 
    # 100. It goes back 100 if used in attack() function becuase it is a recursive function i.e every variable gets updated at every loop
    #and we don't want to change i.e update our enemy_health everytime
    print(f'You have encountered the {random_enemy}.\n')
    

battle()
    #Attack Sequence Function
def attack(enemy_health):
    attack_damage = random.randint(1, 100)
   
    while enemy_health > 0:
        initial_player_choice = input('Choose an attack:\n- Spirit Ambush (S)\n- Dark Incantation (D)\n- Morningstar Attack (M)\n- Poisoned Arrow (P)\n>> ').upper()
        if initial_player_choice == 'S' or initial_player_choice == 'D' or initial_player_choice == 'M' or initial_player_choice == 'P':
            enemy_health -= attack_damage
            print(f"Attack successful. Enemy health is now at {enemy_health}")
            #using attack_damage random function here makes the damage to the enemy vary without creating logical bugs
            #cause this while  loop continues and will not change the attack_damage anytime
            #using attack() function changes the attack_damage but the loop continues for an extra iteration
            attack_damage = random.randint(1, 100)
        else:
            print('Invalid choice.')
            attack(enemy_health)
    print("You successfully defeated the enemy.")
attack(enemy_health)
        

you need to setup health of the enemy only once (using it in attack() sets it up to 100 in every iteration of the loop) making health to fluctuate.Also inherit it into the function not to cause any variable scope related issues

1 Like