Python Forum

Full Version: My while statement is running twice.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm brand new to programming and wanted to write a simple Pokemon game in Python. I got to the point where you can choose a Pokemon and it generates a random wild pokemon encounter, but for some reason, my while statement to see if you want to fight or run asks you twice and I have no idea why!

thanks :D

import random
import time
from typing import List

wild_pokemon = ["Caterpie", "Metapod", "Butterfree", "Weedle"]


class bulbasaur:
    name = "Bulbasaur"
    health = 100
    moves = ["Cut", "Razor Leaf"]


class charmander:
    name = "Charmander"
    health = 100
    moves = ["Tackle", "Ember"]


class squirtle:
    name = "Squirtle"
    health = 100
    moves = ["Tackle", "Bubble"]


class pikachu:
    name = "Pikachu"
    health = 100
    moves = ["Quick Attack", "Thundershock"]


def intro():
    print("Hello, there. Glad to meet you!")
    time.sleep(2)
    print("Welcome to the world of Pokémon!")
    time.sleep(2)
    print("My name is OAK. People affectionately refer to me as the Pokémon Professor.")
    time.sleep(4)
    print("This world is inhabited far and wide by creatures called Pokémon.")
    print("For some people, Pokémon are pets. Others use them for battling.")
    time.sleep(4)
    print("As for myself... I study Pokémon as a profession.")
    time.sleep(4)
    print("But first, tell me a little about yourself...")
    # Ask player for name
    player_name = input("What is your name? \n")
    print(player_name + "! " + "Your very own Pokémon legend is about to unfold!")


def part_one():
    # Prompt Pokémon selection
    player_pokemon = input("What Pokémon will you choose: Bulbasaur, Charmander, or Squirtle?: \n")

    # Validation
    while player_pokemon.lower() != "bulbasaur" and player_pokemon != "charmander" and player_pokemon != "squirtle":
        player_pokemon = input("What Pokémon will you choose: Bulbasaur, Charmander, or Squirtle? ")

    print("You have chosen", player_pokemon.capitalize())
    time.sleep(1)
    print('\n')
    print('...')
    print('\n')
    time.sleep(1)

    # Opponent Pokémon selection
    opponent = random.choice(wild_pokemon)
    print("Wild", opponent, "appeared!")
    time.sleep(1.5)
    print("what will", player_pokemon, "do?\n")

# this is the part that I need help with :)


def choice():
    path = ""
    while path != "fight" and path != "run":
        path =  input("what will you do?\n(FIGHT or RUN)\n")
        if path == "fight":
            print("You have chosen to fight!")
        elif path == "run":
            print("You have chosen to run")

    return path


def check_battle():
    if choice() == "fight":
        in_battle = True
    else:
        in_battle = False
    print()


part_one()
choice()
check_battle()


input("Press enter to close")
It runs twice because you call it twice. First you call choice on line 95. Then you call check_battle on line 96, which calls choice on line 87.

You are part way to the solution already, because you are returning path from the choice function. You need to assign that to a variable on line 87, and pass it to check_battle on line 96:

path = choice()
check_battle(path)
Then you need to add the path parameter to the check_battle definition on line 86, and use that parameter instead of the call to choice on line 87.

I don't know where you are going with this, but note that in_battle will not be available outside of the check_battle function. You might want to check out the functions tutorial linked to in my signature, below. If you're pokemon classes are not going to get more complicated, you might also want to check into named tuples. They'd be an easier way to handle what you've got.
it runs twice because you ask twice.

Quote:
def check_battle():
    if choice() == "fight":
        in_battle = True
    else:
        in_battle = False
    print()
 
 
part_one()
choice()
check_battle()

choice function asks the question. And you execute that function twice, one in check_battle and once before it.
i think you are thinking about this too much like Java. you could code that way but life will be very difficult in Python if you do. Python is going to do what you ask it to do, not what you are thinking. that function named "choice" should be renamed "ask_if_fight_or_run". and it should be called only by a new function named "does_player_want_to_fight_or_run". this new function determines if the player has already been asked. if not, then it calls ask_if_fight_or_run() and saves the answer. then in all cases it gets the saved answer and returns it. then you can call does_player_want_to_fight_or_run() from as many places as needed and the player will be asked just once. i think thats what you meant. maybe i can read your mind a tiny bit but Python does not even try to read your mind.