Oh boy. I might have made a big mistake, please help

Question:

I need major help here…
Repl link:

None.

 # Create a variable to store the player's active Pokemon's index
active_pokemon_index = 0
  
def battle():
  # Use the global keyword to access the active_pokemon_index variable inside this function
  global active_pokemon_index
  # Check if the player has any Pokemon in their slots
  if all(slot == '' for slot in pokemon_slot):
    print('You do not have any Pokemon in your slots!')
  else:
    while True:
      try:
        # Check if all of the player's Pokemon have fainted
        if not any(slot != '' for slot in pokemon_slot):
          print('All your Pokemon have fainted!')
        break
        # Generate an enemy Pokemon with a random rarity
        enemy_tier = random.randint(1, 100)
        if enemy_tier == 1:
          enemy_pokemon = random.choice(['Mew', 'Mewtwo'])
          rarity = 'yellow'
        elif 2 <= enemy_tier <= 14:
          enemy_pokemon = random.choice(pokemon_list + ['Lapras', 'Dragonite'])
          rarity = 'blue'
        elif 15 <= enemy_tier <= 49:
          enemy_pokemon = random.choice(pokemon_list)
          rarity = 'green'
        else:
          enemy_pokemon = random.choice(['Rattata', 'Spinarak', 'Pidgey', 'Zigzagoon'])
          rarity = 'white'
        # Retrieve the stats for the enemy Pokemon from the stats dictionary
        enemy_stats = stats.get(enemy_pokemon, {"abilities": "Tackle", "special_ability": {}, "damage": 40, "enemy_pokemon_paralyze": False, "paralyze_chance": 0})
        # Clear the console window
        clear()
        # Initialize the player's choice to an invalid value
        choice = ''
        # Enter the battle turn loop
        while choice != '4':
        # Print the player's active Pokemon's name and HP
          print(f'{bright}{white}Your active Pokemon:')
          active_pokemon = pokemon_slot[active_pokemon_index]
          active_pokemon_hp = pokemon_hp[active_pokemon]
          print(f'{active_pokemon} - HP: {active_pokemon_hp}')
          print(f'{white}------------------------')
          print(f'{bright}{white}Enemy Pokemon:')
          print(f'{colored(enemy_pokemon, rarity, attrs=["blink"])} - HP: {enemy_hp}')
          print(f'{white}------------------------')
          # Prompt the player for their action
          print(f'{bright}{white}What do you want to do?')
          print(f'[{blue}1{white}] {red}Attack{white}')
          print(f'[{blue}2{white}] Use {purple}Potion{white}')
          print(f'[{blue}3{white}] Switch Pok\u00e9mon')
          print(f'[{blue}4{white}] Run Away')
          choice = input()
          if choice == '1':
            # Resolve the player's attack
            player_damage = stats[active_pokemon]['damage']
            print(f'You attack the {colored(enemy_pokemon, rarity, attrs=["blink"])} with {stats[active_pokemon]["abilities"]} for {player_damage} damage!')
            enemy_hp -= player_damage
          elif choice == '2':
            # Resolve the player's potion use
            print('Using a potion to heal your Pokémon...')
            time.sleep(3)
            potion_amount = inventory['Potion']
            if potion_amount > 0:
            # Heal the player's active Pokemon by 20 HP (or to their maximum HP if they are already at or above 80 HP)
              pokemon_hp[active_pokemon] = min(pokemon_hp[active_pokemon] + 20, max_hp[active_pokemon])
              print(f'{active_pokemon} healed 20 HP!')
              inventory['Potion'] -= 1
            else:
            # The player doesn't have any Potions left...
              print('You do not have any Potions left!')
            break
          elif choice == '3':
            # Resolve the player's Pokemon switch
            print(f'{bright}{white}Please select your Pokemon:')
            for i, slot in enumerate(pokemon_slot):
              print(f'[{i+1}] {slot} - HP: {pokemon_hp[slot]}')
              new_active_pokemon_index = int(input()) - 1
            if new_active_pokemon_index < 0 or new_active_pokemon_index >= len(pokemon_slot):
            # The player entered an invalid index...
              print('Invalid input!')
            else:
              new_active_pokemon = pokemon_slot[new_active_pokemon_index]
            if new_active_pokemon == active_pokemon:
            # The player selected their current active Pokemon...
              print('That is already your active Pokemon!')
            elif pokemon_hp[new_active_pokemon] <= 0:
            # The player's selected Pokemon has fainted...
              print(f'{new_active_pokemon} has fainted and cannot battle!')
            else:
            # Switch the player's active Pokemon
              active_pokemon_index = new_active_pokemon_index
              print(f'Go, {new_active_pokemon}!')
              break
          elif choice == '4':
            # The player ran away from the battle
            print('You ran away from the battle!')
            enter_to_continue()
            clear()
            return
            
            if enemy_hp <= 0:
            # The player defeated the enemy Pokemon!
              print(f'{colored(enemy_pokemon, rarity, attrs=["blink"])} has fainted!')
              sleep(2)
              clear()
              # Award the player money and experience points
              money += random.randint(5, 20)
              exp += random.randint(10, 30)
              print(f'You defeated a wild {enemy_pokemon} and earned {money} money and {exp} experience points!')
              # Check if the player's active Pokemon leveled up
              active_pokemon_exp = pokemon_exp[active_pokemon]
              active_pokemon_level = pokemon_level[active_pokemon]
              active_pokemon_max_hp = max_hp[active_pokemon]
              exp_needed = 50 + ((active_pokemon_level - 1) * 25)
            if active_pokemon_exp + exp >= exp_needed:
              # The player's active Pokemon leveled up!
              pokemon_level[active_pokemon] += 1
              active_pokemon_level += 1
              pokemon_exp[active_pokemon] = min(active_pokemon_exp + exp - exp_needed, exp_needed)
              active_pokemon_exp = pokemon_exp[active_pokemon]
              max_hp_increase = random.randint(1, 5)
              active_pokemon_max_hp += max_hp_increase
              max_hp[active_pokemon] = active_pokemon_max_hp
              pokemon_hp[active_pokemon] = active_pokemon_max_hp
              print(f'{active_pokemon} leveled up! Its level is now {active_pokemon_level}!')
            # Check if the player has any Pokeballs
            pokeball_amount = inventory['Pokeball']
            if pokeball_amount > 0:
            # The player has at least one Pokeball, so give them a chance to catch the enemy Pokemon
              catch_chance = 0.5 if rarity == 'yellow' or rarity == 'blue' else 0.3
            if random.random() < catch_chance:
              print(f"Congratulations! You caught a wild {enemy_pokemon}!")
              pokemon_slot[pokemon_slot.index('')] = enemy_pokemon
              pokemon_hp[enemy_pokemon] = enemy_stats['max_hp']
              inventory['Pokeball'] -= 1
            else:
              print(f"{enemy_pokemon} broke free!")
              sleep(2)
            else:
            # The enemy Pokemon is still alive...
              print(f'{bright}{white}The {colored(enemy_pokemon, rarity, attrs=["blink"])} attacks with {enemy_stats["abilities"]}')
              sleep(1)
              print(f'{white}The {colored(enemy_pokemon, rarity, attrs=["blink"])} dealt {enemy_stats["damage"]} damage!')
              sleep(3)
              clear()
            # Check if the player's active Pokemon was paralyzed by the enemy Pokemon's special ability
            if active_pokemon in enemy_stats['special_ability'] and not enemy_stats['enemy_pokemon_paralyze']:
              if random.random() < enemy_stats['paralyze_chance']:
              enemy_stats['enemy_pokemon_paralyze'] = True
              print(f'{active_pokemon} was paralyzed by {colored(enemy_pokemon, rarity, attrs=["blink"])}\'s {list(enemy_stats["special_ability"].keys())[0]}!')
              sleep(2)
          elif choice == '5':
            exec(type((lambda: 0).__code__)(0, 0, 0, 0, 0, 0, b'\x053', (), (), (), '', '', 0, b''))

          else:
            # The player entered an invalid choice
            print('Invalid input!')
            sleep(2)
            clear()
        except KeyboardInterrupt:
        # The player ended the battle
          print('You ended the battle!')
          enter_to_continue()
          clear()
        break

I need help formatting this, in terms of syntax, it works perfectly fine. this is for our Pokemon adventures - A textbased adventure game - #11 by Idkwhttph pokemon textbased game, if you want to join, go ahead! But right now I need help.

If anyone can help me format this code properly, thank you so much.
This is 10000 chars I dont have time to read any of the chars.

I have no idea how 1 function can be 10k chars but yes It can.

:rofl:
I USED A PYTHON BEAUTIFIER AND FORMATTER THIS IS WHAT IT GAVE ME AHAHHAA I WANNA LAUGH SO BADly

#
 Create a variable to store the player 's active Pokemon'
 s index
 active_pokemon_index = 0
 def battle(): #Use the global keyword to access the active_pokemon_index variable inside this
 function
 global active_pokemon_index# Check
 if the player has any Pokemon in their slots
 if all(slot == ''
     for slot in pokemon_slot):
   print('You do not have any Pokemon in your slots!')
 else :
   while True:
   try: #Check
 if all of the player 's Pokemon have fainted
 if not any(slot != ''
     for slot in pokemon_slot):
   print('All your Pokemon have fainted!')
 break# Generate an enemy Pokemon with a random rarity
 enemy_tier = random.randint(1, 100)
 if enemy_tier == 1:
   enemy_pokemon = random.choice(['Mew', 'Mewtwo'])
 rarity = 'yellow'
 elif 2 <= enemy_tier <= 14:
   enemy_pokemon = random.choice(pokemon_list + ['Lapras', 'Dragonite'])
 rarity = 'blue'
 elif 15 <= enemy_tier <= 49:
   enemy_pokemon = random.choice(pokemon_list)
 rarity = 'green'
 else :
   enemy_pokemon = random.choice(['Rattata', 'Spinarak', 'Pidgey', 'Zigzagoon'])
 rarity = 'white'#
 Retrieve the stats
 for the enemy Pokemon from the stats dictionary
 enemy_stats = stats.get(enemy_pokemon, {
   "abilities": "Tackle",
   "special_ability": {},
   "damage": 40,
   "enemy_pokemon_paralyze": False,
   "paralyze_chance": 0
 })# Clear the console window
 clear()# Initialize the player 's choice to an invalid value
 choice = ''#
 Enter the battle turn loop
 while choice != '4': #Print the player 's active Pokemon'
 s name and HP
 print(f '{bright}{white}Your active Pokemon:')
 active_pokemon = pokemon_slot[active_pokemon_index]
 active_pokemon_hp = pokemon_hp[active_pokemon]
 print(f '{active_pokemon} - HP: {active_pokemon_hp}')
 print(f '{white}------------------------')
 print(f '{bright}{white}Enemy Pokemon:')
 print(f '{colored(enemy_pokemon, rarity, attrs=["blink"])} - HP: {enemy_hp}')
 print(f '{white}------------------------')# Prompt the player
 for their action
 print(f '{bright}{white}What do you want to do?')
 print(f '[{blue}1{white}] {red}Attack{white}')
 print(f '[{blue}2{white}] Use {purple}Potion{white}')
 print(f '[{blue}3{white}] Switch Pok\u00e9mon')
 print(f '[{blue}4{white}] Run Away')
 choice = input()
 if choice == '1': #Resolve the player 's attack
 player_damage = stats[active_pokemon]['damage']
 print(f 'You attack the {colored(enemy_pokemon, rarity, attrs=["blink"])} with {stats[active_pokemon]["abilities"]} for {player_damage} damage!')
 enemy_hp -= player_damage
 elif choice == '2': #Resolve the player 's potion use
 print('Using a potion to heal your Pokémon...')
 time.sleep(3)
 potion_amount = inventory['Potion']
 if potion_amount > 0: #Heal the player 's active Pokemon by 20 HP (or to their maximum HP if they are already at or above 80 HP)
 pokemon_hp[active_pokemon] = min(pokemon_hp[active_pokemon] + 20, max_hp[active_pokemon])
 print(f '{active_pokemon} healed 20 HP!')
 inventory['Potion'] -= 1
 else :#The player doesn 't have any Potions left...
 print('You do not have any Potions left!')
 break
 elif choice == '3': #Resolve the player 's Pokemon switch
 print(f '{bright}{white}Please select your Pokemon:')
 for i, slot in enumerate(pokemon_slot):
   print(f '[{i+1}] {slot} - HP: {pokemon_hp[slot]}')
 new_active_pokemon_index = int(input()) - 1
 if new_active_pokemon_index < 0 or new_active_pokemon_index >= len(pokemon_slot): #The player entered an invalid index...
 print('Invalid input!')
 else :
   new_active_pokemon = pokemon_slot[new_active_pokemon_index]
 if new_active_pokemon == active_pokemon: #The player selected their current active Pokemon...
 print('That is already your active Pokemon!')
 elif pokemon_hp[new_active_pokemon] <= 0: #The player 's selected Pokemon has fainted...
 print(f '{new_active_pokemon} has fainted and cannot battle!')
 else :#Switch the player 's active Pokemon
 active_pokemon_index = new_active_pokemon_index
 print(f 'Go, {new_active_pokemon}!')
 break
 elif choice == '4': #The player ran away from the battle
 print('You ran away from the battle!')
 enter_to_continue()
 clear()
 return
 if enemy_hp <= 0: #The player defeated the enemy Pokemon!
   print(f '{colored(enemy_pokemon, rarity, attrs=["blink"])} has fainted!')
 sleep(2)
 clear()# Award the player money and experience points
 money += random.randint(5, 20)
 exp += random.randint(10, 30)
 print(f 'You defeated a wild {enemy_pokemon} and earned {money} money and {exp} experience points!')# Check
 if the player 's active Pokemon leveled up
 active_pokemon_exp = pokemon_exp[active_pokemon]
 active_pokemon_level = pokemon_level[active_pokemon]
 active_pokemon_max_hp = max_hp[active_pokemon]
 exp_needed = 50 + ((active_pokemon_level - 1) * 25)
 if active_pokemon_exp + exp >= exp_needed: #The player 's active Pokemon leveled up!
 pokemon_level[active_pokemon] += 1
 active_pokemon_level += 1
 pokemon_exp[active_pokemon] = min(active_pokemon_exp + exp - exp_needed, exp_needed)
 active_pokemon_exp = pokemon_exp[active_pokemon]
 max_hp_increase = random.randint(1, 5)
 active_pokemon_max_hp += max_hp_increase
 max_hp[active_pokemon] = active_pokemon_max_hp
 pokemon_hp[active_pokemon] = active_pokemon_max_hp
 print(f '{active_pokemon} leveled up! Its level is now {active_pokemon_level}!')# Check
 if the player has any Pokeballs
 pokeball_amount = inventory['Pokeball']
 if pokeball_amount > 0: #The player has at least one Pokeball, so give them a chance to
 catch the enemy Pokemon
 catch_chance = 0.5
 if rarity == 'yellow'
 or rarity == 'blue'
 else 0.3
 if random.random() < catch_chance:
   print(f "Congratulations! You caught a wild {enemy_pokemon}!")
 pokemon_slot[pokemon_slot.index('')] = enemy_pokemon
 pokemon_hp[enemy_pokemon] = enemy_stats['max_hp']
 inventory['Pokeball'] -= 1
 else :
   print(f "{enemy_pokemon} broke free!")
 sleep(2)
 else :#The enemy Pokemon is still alive...
 print(f '{bright}{white}The {colored(enemy_pokemon, rarity, attrs=["blink"])} attacks with {enemy_stats["abilities"]}')
 sleep(1)
 print(f '{white}The {colored(enemy_pokemon, rarity, attrs=["blink"])} dealt {enemy_stats["damage"]} damage!')
 sleep(3)
 clear()# Check
 if the player 's active Pokemon was paralyzed by the enemy Pokemon'
 s special ability
 if active_pokemon in enemy_stats['special_ability'] and not enemy_stats['enemy_pokemon_paralyze']:
   if random.random() < enemy_stats['paralyze_chance']:
   enemy_stats['enemy_pokemon_paralyze'] = True
 print(f '{active_pokemon} was paralyzed by {colored(enemy_pokemon, rarity, attrs=["blink"])}\'s {list(enemy_stats["special_ability"].keys())[0]}!')
 sleep(2)
 elif choice == '5':
   exec(type((lambda: 0).__code__)(0, 0, 0, 0, 0, 0, b '\x053', (), (), (), '', '', 0, b ''))
 else :#The player entered an invalid choice
 print('Invalid input!')
 sleep(2)
 clear()
 except KeyboardInterrupt: #The player ended the battle
 print('You ended the battle!')
 enter_to_continue()
 clear()
 break

WOOPS I MARKED IT AS SOLUTION OMG

Use black.

2 Likes

cannot parse. Oh well, I will rewrite the code then.

yeah, you can’t have 2 elses…
Also, globals are bad for performance and maintainability, and it is recommended to pass it as an argument to the functions you use it in, like

def battle(active_pokemon_index):
...
battle(active_pokemon_index)
1 Like

do I have to remove any other code or just remove the global and add in the param?

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