1

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())
3
  • What does it mean the while loop doesn't loop? also there are many undefined variables here. Please work out a minimal reproducible example Commented May 27, 2020 at 16:13
  • Cannot reproduce. The loop as shown is entered. Commented May 27, 2020 at 16:14
  • 1
    There was an answer that was deleted that pointed to another problem with your code, which is that the if checks for player_choice should be changed to if/elif/else. Right now your function will return if the player chooses the first option Commented May 27, 2020 at 16:24

2 Answers 2

3

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())
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.