Jack
Jack

Reputation: 39

How to execute multiple while loops?

I was wondering if someone could tell me how to have the computer choose a new choice after every round. I got the lower portion of the code to cover all choices but it turns out my code runs where the computer uses the same choice every time. Is there a way to set my code so the computer chooses something new from the list. Thanks!

import random

def computerChoice():
    gameList = ["Rock", "Paper", "Scissors"]
    computerChoice = random.choice(gameList)
    print(computerChoice)




def userChoice(computerChoice):
    userScore = 0
    computerScore = 0
    rounds = 0
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        if userAnswer == "Rock" and computerChoice != "Paper":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice != "Scissors":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice != "Rock":
            userScore += 1
            rounds += 1
            print("The Computer Loses and You Win the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Rock" and computerChoice == "Paper":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice == "Scissors":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice == "Rock":
            computerScore += 1
            rounds += 1
            print("The Computer Wins and You Lose the Round!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Rock" and computerChoice == "Rock":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Scissors" and computerChoice == "Scissors":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        elif userAnswer == "Paper" and computerChoice == "Paper":
            print("This round is a PUSH!")
            print("You have a score of " + str(userScore) + " and the computer has a score of " + str(computerScore))
            continue
        else:
            print("Whatever you just inputed doesn't work noodlehead, try again!")
            continue

x = computerChoice()
print(userChoice(x))

Upvotes: 0

Views: 111

Answers (2)

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

Move this inside your while loop:

computerChoice = random.choice(gameList)

Currently you are saving a single choice, and then using it every time. Where as this would create a new choice each time:

while userScore < 2 and computerScore < 2:
    userAnswer = input("Please choose Rock, Paper, or Scissors: ")
    computerChoice = random.choice(gameList)
    # Compare to see who won.

Note that gameList must be available inside that scope, so you will have to either pass it in as a function parameter or include it inside the function. That does change the nature of the function somewhat:

def game():
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    userScore, computerScore, rounds = game_loop()

def game_loop(available_options: list=["Rock", "Paper", "Scissors"]):
    computerScore = 0
    userScore = 0
    rounds = 0
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        computerChoice = random.choice(available_options)
        // compare and score (preferably in their own functions)

    return userScore, computerScore, rounds

Upvotes: 1

R Nar
R Nar

Reputation: 5515

try this

import random

def computerChoice():
    gameList = ["Rock", "Paper", "Scissors"]
    return random.choice(gameList)

def userChoice(computerChoice):
    userScore = 0
    computerScore = 0
    rounds = 0
    print("Welcome to Rock, Paper, Scissors. You are playing against the computer on best out of three game, winner takes all! Have Fun!")
    while userScore < 2 and computerScore < 2:
        userAnswer = input("Please choose Rock, Paper, or Scissors: ")
        #all your if statements and stuff
        computerChoice = computerChoice()

print(userChoice(computerChoice()))

Upvotes: 0

Related Questions