Skip to content Skip to sidebar Skip to footer

Python 3 Rock, Paper, Scissors Issue

I am working on a rock, paper, scissors game for a programming homework assignment and I have run into a little snag. The program is suppose run by the user selecting 1 of 4 option

Solution 1:

For issue 1, it's because you set the computer and player choices before your loop, and never update them. Change the beginning of your loop to:

while play_again == 'y' or play_again == 'Y':
    computer_choice = process_computer_choice()
    player_choice = process_player_choice()

You can also remove the lines of code before the loop that check the inputs and winner, as it's technically redundant for the first round.

For issue 2, just add the results after a 4 is chosen, like so:

if player_choice == 4:
      print('Exiting program....')  
      print('there was', tied_games, 'tied games.')
      print('the player won', player_games, 'games.')
      print('The computer won', computer_games,'games.')
      sys.exit() # be sure you add 'import sys' to the beginning of your file

Also, the line in your main loop determine_winner(player_choice, computer_choice) is indented so it will only be called if the computer chooses scissors, so you should unindent that :)


Solution 2:

You aren't assigning to computer_choice or player_choice again, but using it's value.

while play_again == 'y' or play_again == 'Y':

    process_computer_choice()

    process_player_choice()

Should be

while play_again == 'y' or play_again == 'Y':

    computer_choice = process_computer_choice()

    player_choice = process_player_choice()

As for quitting just break in the quit choice. You have to return early from the process_player_choice and also do something in main.

So in process_player_choice:

if player_choice == 4:
    print('Exiting program....')
    return

and in main: player_choice = process_player_choice()

if player_choice == 4:
     return 

winner = determine_winner(player_choice, computer_choice)

#setup while loop for playing multiple games
while play_again == 'y' or play_again == 'Y':

    computer_choice = process_computer_choice()

    player_choice = process_player_choice()
    if player_choice == 4:
         break

Post a Comment for "Python 3 Rock, Paper, Scissors Issue"