How to make my second "while True" statement work the same as my first one

82 Views Asked by At

On this coding project that I'm doing, my first "while True" statement repeats an input question. My second one doesn't. When someone doesn't enter one of the types on the second one, it's supposed to say: print("You didn't actually enter a type.\nTry Again.") And then repeat this input question: random_mon_name = input("Okay, what is the first type that your Pokemon has?") Instead it repeats the print statement infinitely.

I know that I definitely messed up somewhere, can someone help me or point me in the right direction?

Also, I'm doing this in CodeSkulptor3 which uses python 3.

This is the full code:

import random

print("Time to make a Pokemon!")

pokemontype = ["Normal" , "Fire" , "Water" , "Grass" , "Electric" , "Ice" , "Fighting" , "Poison" , "Ground" , "Flying" , "Psychic" , "Bug" , "Rock" , "Ghost" , "Dark" , "Dragon" , "Steel" , "Fairy"
]

def poke_type():
    while True:
    
        yorn = input ("Would you like your pokemon to have 1 or 2 types?")

        if (yorn =='1' or yorn =='1 ' or yorn.lower() =='one' or yorn.lower() =='one '):
            type1 = random.choice(pokemontype)
            print("You chose for your Pokemon to have 1 type.\nThe type that you got is " + type1 +".")
            break 
    
        elif(yorn =='2' or yorn =='2 ' or yorn.lower() =='two'or yorn.lower() =='two '):
            type1 = random.choice(pokemontype)
            type2 = random.choice(pokemontype)
            print("You chose for your Pokemon to have 2 types.\nThose types are "+ type1 + " and " +type2 +".")
            break 
        else:
            print("You didn't answer correctly, try again.")
            continue
        return
poke_type()    
    
mon_name=input("Now time to move onto your Pokemon's name, would you like to give your Pokemon its own name, or have it randomly generated based of the typing your Pokemon has?\n(Enter \"My own\" or \"Random\")")

if (mon_name.lower()=='my own'):
    own_mon_name = input("Great, what name are you going to give your Pokemon?")
    mon_name_YORN = input("Are you sure you want to name you Pokemon " + own_mon_name + "?\n(Enter \"yes\" or \"no\")")
    if (mon_name_YORN.lower()=='yes' or 'yes '):
        print("Great, now time to move on to the next part.")
    elif (mon_name_YORN.lower()=='no' or 'no '):
        ANOTHER_MON_NAME = input("Okay, what do you want to name your Pokemon?\n(Think on this one because your not getting another chance to change it.)")
        
elif (mon_name.lower()=='random'):
    random_mon_name = input("Okay, what is the first type that your Pokemon has?")
    
    mon_name_type_NORMAL = ['Normodo' , 'Normalia' , 'Normew']
    mon_name_type_FIRE = ['Firaint' , 'Heatoro' , 'Flamala']
    mon_name_type_WATER = ['Splashoto' , 'Oceatto', 'Puddlio']
    mon_name_type_GRASS = ['Weedevon' , 'Grassimar' , 'Leafadroon']
    mon_name_type_ELECTRIC = ['Enetick' , 'Electol' , 'Litero']
    mon_name_type_ICE = ['Snova' , 'Icello' , 'Colaggro']
    mon_name_type_FIGHTING = ['Allphist','Lullakick','Bubblow']
    mon_name_type_POISON = ['Poizase' , 'Gunkamight' , 'Aison']
    mon_name_type_GROUND = ['Groutal' , 'Moundiflox' , 'Umbron']
    mon_name_type_FLYING = ['Wingyu' , 'Sparial' , 'Flogun']
    mon_name_type_PSYCHIC = ['Mindswell' , 'Thinkarva' , 'Psymarine']
    mon_name_type_BUG = ['Bugonite' , 'Flyzaur' , 'Mantile']
    mon_name_type_GHOST = ['Phantini' , 'Spirook' , 'Tortasm']
    mon_name_type_ROCK = ['Storilot' , 'Rocoalia' , 'Sedite']
    mon_name_type_DARK = ['Nolite' , 'Dariotte' , 'Nitrash']
    mon_name_type_DRAGON = ['Dracat' , 'Wyveron' , 'Randigon']
    mon_name_type_STEEL = ['Metalaze' , 'Gearevo' , 'Iromonora']
    mon_name_type_FAIRY = ['Faeream' , 'Pixirola' , 'Trixitrite']


def loop_for_ranmonname():
    while True:
        if (random_mon_name.lower()=='normal'):
            rangen_mon_name = random.choice(mon_name_type_NORMAL)
            break
        elif (random_mon_name.lower()=='fire'):
            rangen_mon_name = random.choice(mon_name_type_FIRE)
            break
        elif (random_mon_name.lower()=='water'):
            rangen_mon_name = random.choice(mon_name_type_WATER)
            break
        elif (random_mon_name.lower()=='grass'):
            rangen_mon_name = random.choice(mon_name_type_GRASS)
            break
        elif (random_mon_name.lower()=='electric'):
            rangen_mon_name = random.choice(mon_name_type_ELECTRIC)
            break
        elif (random_mon_name.lower()=='ice'):
            rangen_mon_name = random.choice(mon_name_type_ICE)
            break
        elif (random_mon_name.lower()=='fighting'):
            rangen_mon_name = random.choice(mon_name_type_FIGHTING)
            break
        elif (random_mon_name.lower()=='poison'):
            rangen_mon_name = random.choice(mon_name_type_POISON)
            break
        elif (random_mon_name.lower()=='ground'):
            rangen_mon_name = random.choice(mon_name_type_GROUND)
            break
        elif (random_mon_name.lower()=='flying'):
            rangen_mon_name = random.choice(mon_name_type_FLYING)
            break
        elif (random_mon_name.lower()=='psychic'):
            rangen_mon_name = random.choice(mon_name_type_PSYCHIC)
            break
        elif (random_mon_name.lower()=='bug'):
            rangen_mon_name = random.choice(mon_name_type_BUG)
            break
        elif (random_mon_name.lower()=='ghost'):
            rangen_mon_name = random.choice(mon_name_type_GHOST)
            break
        elif (random_mon_name.lower()=='rock'):
            rangen_mon_name = random.choice(mon_name_type_ROCK)
            break
        elif (random_mon_name.lower()=='dark'):
            rangen_mon_name = random.choice(mon_name_type_DARK)
            break
        elif (random_mon_name.lower()=='dragon'):
            rangen_mon_name = random.choice(mon_name_type_DRAGON)
            break
        elif (random_mon_name.lower()=='steel'):
            rangen_mon_name = random.choice(mon_name_type_STEEL)
            break
        elif (random_mon_name.lower()=='fairy'):
            rangen_mon_name = random.choice(mon_name_type_FAIRY)
            break
        else:
            print("You didn't actually enter a type.\nTry Again.")
            continue
        return 
loop_for_ranmonname()````

1

There are 1 best solutions below

0
On

loop_for_ranmonname always returns in the first loop; you want to unindent the return keyword as it's inside the while loop or just remove it entirely:

def loop_for_ranmonname():
    while True:
        if (random_mon_name.lower()=='normal'):
            rangen_mon_name = random.choice(mon_name_type_NORMAL)
        #...
        else:
            print("You didn't actually enter a type.\nTry Again.")
            continue
    # Unindented, can be removed
    return 

I would like to point out this code suffers from redundancy issues and you may want to refactor it.