Python Beginner Project- Text-Based RPG


I am having issues with my first personal project. I wanted to put into practice basic things I learned after completing a 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.

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.


#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')
            print('Invalid selection. Please try again.')

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}')
            print('Invalid selection. Please try again.')

def battle():

    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}")
            print('Invalid choice.')

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

def gameplay():


Inside the attack() function, you used the input() function which can take any input from the user.
And about the fact that how enemy_health keeps increasing or decreasing is that you have taken the variable enemy_health as a global variable, so every time the attack() function is called, enemy_health is taken as 100 and attack_damage can be any integer between [1,100] inclusive, this is why it doesn’t only decrease.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.