Can't get the Rock Paper Scissors project to work


#1

Hi guys,

Can somebody help me to get my code to work properly. If I enter a R or P or S it works correctly
but if I enter something else I don’t get the: You can’t do that… Exiting… line. It just crashes.

Thanks in advance!

"""Welcome to today's edition of Rock, Paper, Scissors."""

from random import randint
from time import sleep

options = ["R", "P", "S"]

lose_message = "Ha!! You lose!"
win_message =  "You've won!"

def decide_winner(user_choice, computer_choice):
  print "You selected %s" % (user_choice)
  sleep(1)
  print "Your rival selected %s" % (computer_choice)
  sleep(1)
  user_choice_index = options.index(user_choice)
  computer_choice_index = options.index(computer_choice)
  if user_choice_index == computer_choice_index:
    print "It's a tie."
  elif user_choice_index == 0 and computer_choice_index == 2:
    print win_message
  elif user_choice_index == 1 and computer_choice_index == 0:
    print win_message
  elif user_choice_index == 2 and computer_choice_index == 1:
    print win_message
  elif user_choice_index > 2:
    print "You can't do that... Exiting..."
    return
  else:
    print lose_message

def play_RPS():
  print "Welcome! Let's play Rock, Paper, Scissors!"
  user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ").upper()
  sleep(1)
  computer_choice = options[randint(0, len(options)-1)]
  decide_winner(user_choice, computer_choice)

play_RPS()

#2

The approach suggested by the exercise to handle incorrect guesses is just wrong, and doesn’t work

I would recommend if you can think of a way to handle invalid guesses (this would be a good challenge), of course, if you need i can provide you further assistance, but ideally (given you now know exercise approach sucks) you would attempt a solution yourself first :slight_smile:

good luck! Let me know if further assistance is required

one hint:

user_choice_index = options.index(user_choice)

look into what .index() does when provided with a value that is not in list (you can find this in python documentation)


#3

ok, thanks! I will try to find another way.


#4
"""Welcome to today's edition of Rock, Paper, Scissors."""

from random import randint
from time import sleep

options = ["R", "P", "S"]

lose_message = "Ha!! You lose!"
win_message =  "You've won!"

def decide_winner(user_choice, computer_choice):
  print "You selected %s" % (user_choice)
  sleep(1)
  print "Your rival selected %s" % (computer_choice)
  sleep(1)
  user_choice_index = options.index(user_choice)
  computer_choice_index = options.index(computer_choice)
  if user_choice_index == computer_choice_index:
    print "It's a tie."
  elif user_choice_index == 0 and computer_choice_index == 2:
    print win_message
  elif user_choice_index == 1 and computer_choice_index == 0:
    print win_message
  elif user_choice_index == 2 and computer_choice_index == 1:
    print win_message
  else:
    print lose_message

def play_RPS():
  print "Welcome! Let's play Rock, Paper, Scissors!"
  user_choice = raw_input("Select R for Rock, P for Paper, or S for Scissors: ").upper()
  sleep(1)
  if user_choice != "R" or "P" or "S":
    computer_choice = options[randint(0, len(options)-1)]
    decide_winner(user_choice, computer_choice)
  else:
    print "You can't do that.. Exiting..."

play_RPS()

Could you explain why this is not working? I am still looking into what .index does but it seemed to me that this should also work… I added the if / else statement at the bottom.


#5

but if we look at the official python docs:

https://docs.python.org/3/tutorial/datastructures.html

its says about the index method:

Return zero-based index in the list of the first item whose value is x. Raises a ValueError if there is no such item.

which means, that if item is not present in list, we get an error. Why do you think i said the exercise approach isn’t working?

This leaves us with two options, what do you think this two options are?

here are the two options if you can’t figure it out:

verify item is in list before using .index()
catch the error and handle it