Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why wont it let me select and option (0,1,2). Just automatically selects for me (im new)

+13 votes
asked Sep 2, 2025 by Austin Young (220 points)
import random
from enum import IntEnum

class Action(IntEnum):
    Rock = 0
    Paper = 1
    Scissors = 2

def get_user_selection():
    user_imput = input("Enter a choice (Rock[0], Paper[1], Scissors[2]): ")
    selection = int(user_input)
    action = Action(selection)
    return action
def get_user_selection():
    choices = [f"{action.name}[{action.value}]" for action in Action]
    choices_str = ", ".joint(choices)
    selection = int(input(f"Enter a choice ({choices_str}): "))
    action = Action(selection)
    return action
def get_user_selection():
    selection = random.randint(0, len(Action) - 2)
    action = Action(selection)
    return action
def get_computer_selection():
    selection = random.randint(0, len(Action) - 2)
    action = Action(selection)
    return action
def determine_winner(user_action,computer_action):
    if user_action == computer_action:
        print(f"Both players selected {user_action.name}. It's a Tie!")
    elif user_action == Action.Rock:
        if computer_action == Action.Scissors:
            print("Rock smashes Scissors to pieces! You Win!")
        else:
            print("Paper covers and smothers Rock. You Lose, better luck next time.")
    elif user_action == Action.Paper:
        if computer_action == Action.Rock:
            print("Paper covers and smother Rock! You Win!")
        else:
            print("Scissors cuts Paper to shreads. You Lose, better luck next time.")
    elif user_action == Action.Scissors:
        if computer_action == Action.Paper:
            print("Scissors cuts Paper to shreads! You Win!")
        else:
            print("Rock smashes Scissors to pieces. You Lose, better luck next time.")
            
while True:
    try:
        user_action = get_user_selection()
    except ValueError as e:
        range_str = f"[0, {len(Action) -1}]"
        print(f"Invalid Selection. Enter a Value in Range {range_str}")
        continue
    
    user_action = get_user_selection()
    computer_action = get_computer_selection()
    determine_winner(user_action, computer_action)
    
    play_again = input("Play again!? (y/n): ")
    if play_again.lower() != "y":
        break

1 Answer

+1 vote
answered Sep 3, 2025 by Peter Minarik (101,340 points)

In the beginning, you define your get_user_selection() function like this:

def get_user_selection():
    user_imput = input("Enter a choice (Rock[0], Paper[1], Scissors[2]): ")
    selection = int(user_input)
    action = Action(selection)
    return action

Then, in the next line, you redefine your function like this:

def get_user_selection():
    choices = [f"{action.name}[{action.value}]" for action in Action]
    choices_str = ", ".joint(choices)
    selection = int(input(f"Enter a choice ({choices_str}): "))
    action = Action(selection)
    return action

And even after this, you forget about the previous definitions and you create a new, 3rd definition for your get_user_selection() function:

def get_user_selection():
    selection = random.randint(0, len(Action) - 2)
    action = Action(selection)
    return action

Since the compiler reads the code sequentially, this last definition will be the one that will be used during the execution of your program. This last definition does not take any user input; it just randomizes a value in the allowed range.

I'd recommend thinking about why you have 3 definitions of the same function. Remove what you do not need. Or rename the function. Or comment out the unused variations.

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...