-1

My code is below, it's prompting for the desired info, but if/when user is re-prompted the first time to re-enter number of games, if its not the expected number, I am not sure how to re-loop/keeping asking the question?

print('Welcome to my Rock, Paper, Scissors Game!')
print ()
user_action = eval(input('Enter Number of Games -- Odd Number between 3 and 11:'))
REMAINDER = user_action % 2
if REMAINDER != 0:
if user_action > 3 or user_action < 13:
    print()
    print()
print("Game 1 \n Enter (R)ock, (P)aper, or (S)cissors")
else:
print("\"Sorry, try again. \n Enter the Number of Games -- Off Number 3 to 11")
print
print`enter code here`
user_action = eval(input('Enter Number of Games -- Odd Number between 3 and 11:'))
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

0

Start a If/Elif statement. For eg. 'If' contains the range value for num of games. And 'Elif' contains if user input number is out of range. Then in the 'Elif' start a 'While Loop'. Inside the loop, ask the user for another input. to check if this input is correct or not, do If/Elif once again with same conditions as before. This time if the 'If' is satisfied, 'break' the loop. If the 'Elif' is satisfied, make an increment in the 'While Loop' variable.

What this will do? First it will check if the input is in range or not. If Yes, then no problem. If No, then it will start a loop which will keep asking the user for correct input. Once it gets the correct input, it will break out from that loop and you get a perfectly well-defined input.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aryan
  • 21
  • 2
0

Maybe you could create like a helper function called something like get_int_input that handles the vailidation:

from random import randint

MIN_NUM_GAMES = 3
MAX_NUM_GAMES = 11
CHOICES = ['rock', 'paper', 'scissors']
LOSER_TO_WINNER = {'rock': 'paper', 'scissors': 'rock', 'paper': 'scissors'}

def rock_paper_scissors():
    user_choice = CHOICES[get_int_input('\nEnter 1 for rock, 2 for paper, or 3 for scissors: ', 1, 3) - 1]
    computer_choice = CHOICES[randint(0, 2)]
    if user_choice == computer_choice:
        print(f'It is a draw, both you and the computer chose {user_choice}...')
        return 0  # draw
    elif LOSER_TO_WINNER[computer_choice] == user_choice:
        print(f'You win! The computer threw {computer_choice} while you chose {user_choice}.')
        return 1  # win
    else:
        print(f'You lose... The computer threw {computer_choice} while you chose {user_choice}.')
        return -1  # loss

def get_int_input(prompt, min_num, max_num):
    num = -1
    while True:
        try:
            num = int(input(prompt))
            if min_num <= num <= max_num:
                break
            print(f'Error: Integer outside of allowed range {min_num}-{max_num} inclusive, try again...')
        except ValueError:
            print('Error: Enter an integer, try again...')
    return num

def main():
    print('Welcome to my Rock, Paper, Scissors Game!\n')
    num_games = get_int_input(f'Enter the number of games: ', MIN_NUM_GAMES, MAX_NUM_GAMES)
    num_wins = num_losses = num_draws = 0
    for _ in range(num_games):
        game_outcome = rock_paper_scissors()
        if game_outcome == 1:
            num_wins += 1
        elif game_outcome == -1:
            num_losses += 1
        else:
            num_draws += 1
    print(f'\nYou played {num_games} games, with {num_wins} win(s), {num_losses} loss(es), {num_draws} draw(s).')
    print('Goodbye!')

if __name__ == '__main__':
    main()

Example Usage:

Welcome to my Rock, Paper, Scissors Game!

Enter the number of games: 2
Error: Integer outside of allowed range 3-11 inclusive, try again...
Enter the number of games: 12
Error: Integer outside of allowed range 3-11 inclusive, try again...
Enter the number of games: 4

Enter 1 for rock, 2 for paper, or 3 for scissors: 3
You lose... The computer threw rock while you chose scissors.

Enter 1 for rock, 2 for paper, or 3 for scissors: 2
You win! The computer threw rock while you chose paper.

Enter 1 for rock, 2 for paper, or 3 for scissors: 1
It is a draw, both you and the computer chose rock...

Enter 1 for rock, 2 for paper, or 3 for scissors: 2
It is a draw, both you and the computer chose paper...

You played 4 games, with 1 win(s), 1 loss(es), 2 draw(s).
Goodbye!
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • This helps, thank you. How would I restrict the user from entering an odd number in-between 3 and 11? – Jermaine Jenkins Sep 19 '21 at 10:10
  • Add that requirement to the `if min_num <= num <= max_num:` line. Can you think of the [predicate](https://www.google.com/search?q=predicate+computer+science) to achieve that? – Sash Sinha Sep 20 '21 at 00:29