Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Repeat a function 3 times

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading