And/Or usage


#1
user_choice = raw_input("Enter Rock, Paper, or Scissors: ").upper()
  if user_choice == "ROCK" and "PAPER" and "SCISSORS":

When i’m using and between it works fine, but not with or. Isn’t it that with and you have to get all 3 results at the same time? But with or only one of them has to be there?

thank you


#2

its not working properly:

image

i enter paper, which is valid, so the print statement should run, which is not happening

or has the opposite problem:

image

its allowing me to continue with invalid input.

the problem is that you are do no comparison by paper and scissors, so python will just evaluate the strings as true


#3

Well this is the full code

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 choose %s!" % (user_choice)
  print "Computers choice %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: ").upper()
  if user_choice == "ROCK" or "PAPER" or "SCISSORS":
    computer_choice = options[randint(0, 2)]
  
    decide_winner(user_choice, computer_choice)
  else:
    print "%s isnt correct choice" % (user_choice)
play_RPS()

how do i correctly put it together with options list?


#4

using options list is possible, either with in keyword or a for loop

using if multiple conditions is also possible, but then user_choice needs to compared to all 3 strings (rock, paper and scissors)


#5

Thank you, got it wight with in


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.