Additional test need help

Okay!

So I’m doing okay I believe. I’ve introduced a feature to keep the game going until prompted to stop. Just so I could test it without having to constantly run it in bash again, and for entertainment purposes for the “user”.

I was trying to introduce code that checked the input against the available options that register against user_choice. I wanted it to check and read against the input to determine if a viable input was detected. At the moment if you input something outside of a traditional rock, paper or scissors the game says “you lose!”. I wanted it to halt the process, check that input, and if it doesn’t fall within the choices, to print out “incorrect response” before prompting the user if they wanted to play again.

I tried running a small if statement as below.

def play_RPS():
  user_choice = raw_input("Enter Rock, Paper, or Scissors: ")
  user_choice = user_choice.upper()
  if user_choice != options[0: 2]:
    print "Incorrect choice!"
    play_again()
  else:
    computer_choice = options[randint(0, 2)]
    decide_winner(user_choice, computer_choice)
    play_again()

At the moment, I only get the if statement to complete regardless of my input. I can input “rock” and it will still complete as if I have fit the “if” condition.

I have attached my code below for a full understanding of what I’ve modified (to test myself and learn more)

'''

Rock paper scissors game

'''

from random import randint

options = ["ROCK", "PAPER", "SCISSORS"]

message = {

  "tie": "Yawn, it's a tie!",

  "won": "Yay you won!",

  "lost": "Aww you lost!"

}

def decide_winner(user_choice, computer_choice):

  print "You selected: %s" % user_choice

  print "Computer selected: %s" % computer_choice

  if user_choice == computer_choice:

    print message["tie"]

  elif user_choice == options[0] and computer_choice == options[2]:

    print message["won"]

  elif user_choice == options[1] and computer_choice == options[0]:

    print message["won"]

  elif user_choice == options[2] and computer_choice == options[1]:

    print message["won"]

  else:

    print message["lost"]

def play_RPS():

  user_choice = raw_input("Enter Rock, Paper, or Scissors: ")

  user_choice = user_choice.upper()

  computer_choice = options[randint(0, 2)]

  decide_winner(user_choice, computer_choice)

  play_again()

  

def play_again():

  again = raw_input("Play again? Y for yes or N for no: ")

  again = again.upper()

  if again == "Y":

    print

    play_RPS()

  elif again != "N":

    print

    print "I'm sorry, I don't understand"

    play_again()

  else:

    print

    print "Goodbye! Thanks for playing"

play_RPS()
1 Like

maybe you should print(option[0:2]) to see what value this gives, and then figure out why this doesn’t equal the user choice.

So printing this returns only two, instead of three results. I’d forgotten, but I’m not too sure what to do from here. I changed my coding to include options[0:3] which returned all the expected results.

If I run that though, and input “rock” or any of the standard inputs. It still acts as though it’s not one of those options.

From what I can tell, using this code means you will have to satisfy every position in the list.

My original point still stands, you get a list with the options. Which data type is this?

And how do you compare the list of the options with a user choice? The user will choice one of the options (or not, in which case its invalid).

sidenote, what is the point of using list slicing if you want to use the whole list?