1

I would like to implement error handling so that if the user puts in anything besides an integer they get asked for the correct input. I believe try/except would work but I am wondering how I can get it to check for both a correct number within a range and ensuring there are no other characters. I have pasted my code below for review.

Thanks!

# Rock Paper Scissors

import random as rdm
print("Welcome to Rock/Paper/Scissors, you will be up against the computer in a best of 3")

# game_counter = 0

human_1 = input("Please enter your name: ")

GameOptions = ['Rock', 'Paper', 'Scissors']

hmn_score = 0
cpt_score = 0

rps_running = True


def rps():
    global cpt_score, hmn_score
    while rps_running:

        hmn = int(input("""Please select from the following:
                                1 - Rock
                                2 - Paper
                                3 - Scissors
            \n""")) - 1

        while not int(hmn) in range(0, 3):
            hmn = int(input("""Please select from the following:
                                            1 - Rock
                                            2 - Paper
                                            3 - Scissors
                        \n""")) - 1

        print('You Chose: ' + GameOptions[hmn])

        cpt = rdm.randint(0, 2)

        print('Computer Chose: ' + GameOptions[cpt] + '\n')

        if hmn == cpt:
            print('Tie Game!')

        elif hmn == 0 and cpt == 3:
            print('You Win')
            hmn_score += 1
        elif hmn == 1 and cpt == 0:
            print('You Win')
            hmn_score += 1
        elif hmn == 2 and cpt == 1:
            print('You Win')
            hmn_score += 1
        else:
            print('You Lose')
            cpt_score += 1

        game_score()
        game_running()


def game_score():
    global cpt_score, hmn_score
    print(f'\n The current score is {hmn_score} for you and {cpt_score} for the computer \n')


def game_running():
    global rps_running
    if hmn_score == 2:
        rps_running = False
        print(f"{human_1} Wins!")
    elif cpt_score == 2:
        rps_running = False
        print(f"Computer Wins!")
    else:
        rps_running = True


rps()

1 Answer 1

1

To answer your question, you can do something like the following

options = range(1, 4)
while True:
    try:
        choice = int(input("Please select ...etc..."))
        if(choice in options):
            break
        raise ValueError
    except ValueError:
        print(f"Please enter a number {list(options)}")
print(f"You chose {choice}")

As for the a code review, there's a specific stack exchange for that, see Code Review

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I will move this to the appropriate forum section.
No problem, you can mark this as the answer if you believe it is :)

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.