1

I'm trying to create a rock paper scissors text game (as part of my journey to learn coding!). However, I want the user to be able to input 'rock', 'paper', and 'scissors' rather than 0, 1, 2 etc. I keep hitting a stumbling block in correctly returning/printing the result - specifically the draw result. Now I know why this is the case; because 0, 1, 2 does not equal 'rock', 'paper', 'scissors'. What's the best way around this?

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

computer = [scissors, rock, paper]

print("Welcome to Rock, Paper, Scissors!")
user_input = input("Will you choose Scissors, Paper, or Rock?\n").lower()

if user_input == "rock":
  print(rock)
elif user_input == "paper":
  print(paper)
elif user_input == "scissors":
  print(scissors)

computer_choices = random.randint(0, 2)
print("Computer chose: ")
print(computer[computer_choices])

if user_input == computer_choices:
    print ("It's a draw!")
elif user_input == "rock":
  if computer_choices == 2:
    print ("You lose!")
  else: 
    print("You win!")
elif user_input == "paper":
  if computer_choices == 0:
    print("You lose!")
  else: 
    print("You win!")
elif user_input == "scissors":
  if computer_choices == 1:
    print("You lose!")
  else: 
    print("You win!")

That's my code above - how can I learn to fix this?

Thanks in advance all!!

Saladin12
  • 13
  • 3
  • a little unclear what your question is to me. however if a user enters 1 or 3 vs 'rock' you may want to consider validating a users input in a while loop https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Kevin Crum May 18 '23 at 22:00

2 Answers2

1

Use random.choice() to select a random element of a list, and use a list of strings.

computer_choices = random.choice(["rock", "paper", "scissors"])

To print the ASCII art, use a dictionary:

computer = {"rock": rock, "paper", paper, "scissors": scissors}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Perfect, that's helped in fixing the results issue - thank you so much! However, now I'm unable to print the appropriate ASCII art (a rock, if computer chooses a rock) as the strings don't play nice with integers assigned in the list 'computer'... – Saladin12 May 18 '23 at 22:08
  • Use a dictionary: `computer = {"rock": rock, "paper", paper, "scissors": scissors}` – Barmar May 18 '23 at 22:21
0

Although Python's support for Enums is not as pretty as other languages. You can use an Enum, to abstract the hand from the numbers.

from enum import Enum

class Hand(Enum):
    SCISSORS = 0
    ROCK = 1
    PAPER = 2


computer = [Hand.SCISSORS, Hand.ROCK, Hand.PAPER]
xvan
  • 4,554
  • 1
  • 22
  • 37