简体   繁体   中英

Python: while loop doesn't loop

I'm currently creating a card came in python and in creating my draft() function used for drafting the player a deck I encountered a problem with my while loop.

I can't seem to figure out why the while loop doesn't loop.

If anyone can figure out why please do let me know.

player_deck = []

#draft function to draft a player deck should be used when starting game
def draft():
    while len(player_deck) < 10:
        random_variable = random.random()
        if random_variable < 0.2:
            print("Your choise is between Drake and Recovery")
            print("--->   " + drake.description)
            print("--->   " + recovery.description)
            player_choice = input("Which do you choose: ")
            if player_choice.lower() == "drake":
                player_deck.append(drake)
            if player_choice.lower() == "recovery":
                player_deck.append(recovery)
            else:
                return "Please pick one of the two"
        if random_variable >= 0.2 and random_variable <= 0.999999:
            print("Your choise is between Blast Cone and Eminem")
            print("--->   " + blast_cone.description)
            print("--->   " + eminem.description)
            player_choice = input("Which do you choose: ")
            if player_choice.lower() == "blast cone":
                player_deck.append(blast_cone)
            if player_choice.lower() == "eminem":
                player_deck.append(eminem)
            else:
                return "Please pick one of the two"
        return player_deck

print(draft())

The while loop does not repeat because the last return exits the loop before it has a chance to repeat:

def draft():
    while len(player_deck) < 10:
        # bla
        # bla 
        return player_deck

print(draft())

I assume you wanted indent the return differently, maybe

def draft():
    while len(player_deck) < 10:
        # bla
        # bla 
    return player_deck

print(draft())

Your return statement is inside he while loop. As soon as python hits it, the function ends. It should be moved outside of the while loop. Here is the code:

player_deck = []

#draft function to draft a player deck should be used when starting game
def draft():
    while len(player_deck) < 10:
        random_variable = random.random()
        if random_variable < 0.2:
            print("Your choise is between Drake and Recovery")
            print("--->   " + drake.description)
            print("--->   " + recovery.description)
            player_choice = input("Which do you choose: ")
            if player_choice.lower() == "drake":
                player_deck.append(drake)
            if player_choice.lower() == "recovery":
                player_deck.append(recovery)
            else:
                return "Please pick one of the two"
        if random_variable >= 0.2 and random_variable <= 0.999999:
            print("Your choise is between Blast Cone and Eminem")
            print("--->   " + blast_cone.description)
            print("--->   " + eminem.description)
            player_choice = input("Which do you choose: ")
            if player_choice.lower() == "blast cone":
                player_deck.append(blast_cone)
            if player_choice.lower() == "eminem":
                player_deck.append(eminem)
            else:
                return "Please pick one of the two"
    return player_deck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM