Error in rock paper scissors game with Python

Viewed 550

I'm trying to create a rock paper scissors game for a class assignment with Python, but I can't get the result out.

The game is supposed to start with the 1) user's input, 2) show what computer randomly chose, 3) the result (You win, You lose, or Tie with how you won or lost), 4) then ask if you want to Play again, and 5) finally escape.

I did succeed on the first 2 parts, but I can't figure out why 3, 4, 5 is not printing...

The result after showing what the computer played(2), it says 'None' then it loops back to (1) getting the input from the user.

Below is the code. Please advise me on how to fix them. Thank you so much!!

code

import random
while True : 
     player = input("Enter a number (0 for rock, 1 for paper, 2 for scissors) >>")
     RPS = [0,1,2]
     computer = random.choics(RPS)
     RPS_dict = {0:'Rock', 1:'Paper', 2:'Scissors'}
     print("The computer played", RPC_dict.get(computer))

     def game(player, computer):
          RPS_dict = {0:'Rock', 1:'Paper', 2:'Scissors'}
          if player == computer:
              print("Tie!")
          elif player == 0:
               if computer == 1:
                  print("You lose!", RPS_dict.get(computer), 'covers', RPS_dict.get(player))
               else: 
                  print("You win!", RPS_dict.get(player), 'cut', RPS_dict.get(computer))
          elif player == 1:
             if computer == 2:
                  print("You lose!", RPS_dict.get(computer), 'cut', RPS_dict.get(player))
             else:
                  print("You win!", RPS_dict.get(player), 'covers', RPS_dict.get(computer))
     game(player, computer)
     print(game(player, computer))
again = input("Play again? 0 for no, 1 for yes\n")
1 Answers

Your function should be outside your while loop and you don't need to call it within a print statement...

import random



def game(player, computer):
    if player == computer:
        print('game tied')
    else:
        if (player == 1 and computer == 3) or (player == 2 and computer == 1) or (player == 3 and computer == 2):
            print('you win')
        else:
            print('computer wins')

def choice(x):
    y = None
    if x == 1:
        y = 'rock'
    elif x == 2:
        y = 'paper'
    elif x == 3:
        y = 'scissors'
    return y
               
playing = True
while playing == True:
    player_input = int(input("Enter 1 for rock, 2 for paper, 3 for scissors"))
    print('You chose:'+ choice(player_input))
    computer_input = random.choice([1,2,3])
    print('Computer chose:'+ choice(computer_input))
    game(player_input, computer_input)
    restart = int(input("Enter 1 to play again or 2 to quit"))
    if restart == 2:
        playing = False
Related