Repeat a function 3 times

Question:

I’m programming the "Rock, paper, scissors". I want now to run the function I added below 3 times. I tried using a for _ in range(s) but printed the same result 3 times.

import random

OPTIONS = ["Rock", "Paper", "Scissors"]

def get_user_input():
    user_choice = input("Select your play (Rock, Paper or Scissors): ")
    return user_choice

def random_choice():
    computer_choice = random.choice(OPTIONS)
    return computer_choice

def game(user_choice, computer_choice):
    if user_choice == computer_choice:
        print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nDraw.")
    else:
        if computer_choice == "Rock" and user_choice == "Paper":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou won!")
        if computer_choice == "Rock" and user_choice == "Scissors":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou lost...")
        if computer_choice == "Paper" and user_choice == "Scissors":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou won!")
        if computer_choice == "Paper" and user_choice == "Rock":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou lost...")
        if computer_choice == "Scissors" and user_choice == "Rock":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou won!")
        if computer_choice == "Scissors" and user_choice == "Paper":
            print(f"You selected: {user_choice}.nComputer selected: {computer_choice}.nYou lost...")

def main():
    user_choice = get_user_input()
    computer_choice = random_choice()
    for _ in range(3):
        if game(user_choice, computer_choice):
            break
        else:
            print("Game has ended!")

main()

Any tips on how to implement three rounds?

Answers:

I think you have to rework your code in main()

def main():

    for _ in range(3):
        user_choice = get_user_input()
        computer_choice = random_choice()
        game(user_choice, computer_choice)

    print("Game has ended!")

Try something like this

Answered By: MorganMLG

Your program works great! just one small placement error, the random_choice needs to change with every iteration of the loop so I moved the random_choice assignment variable in and that solved the problem. This is also if you want the player to only be able to make one choice.

def main():
    user_choice = get_user_input()
    for _ in range(3):
        computer_choice = random_choice()
        game('Rock', computer_choice)
    print("Game has ended!")

main()

if you want the player to pick every round then it would look like this;

def main():
    for _ in range(3):
        user_choice = get_user_input()
        computer_choice = random_choice()
        game('Rock', computer_choice)
    print("Game has ended!")
            

main()
Answered By: kidcoolness
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.