0

I've coded a simple python rock, paper, scissor game. When I input "Rock" the logic seems to respond as it should. For everything else however, I'm getting some odd behaviour, namely when I put for example:

  • Me: Paper
  • Comp: Scissor

I get a score of: 1, 0 respectively!

I suspect that my if-else implementation is not correct. Anyone able to help me find my error?

# module packages
import random

# data structures and primary variables 
state = ["Rock", "Paper", "Scissor"]
user_points = []
computer_points = []
round_counter = 1

# user input
for i in range(3):
    print("Round " + str(round_counter) + ":")
    computer_choice = random.choice(state)
    print("You: ", end='')
    user_input = input().strip()
    print("Computer: ", end='')
    print(computer_choice + "\n")

# game evaluation logic
if user_input == "Rock" or "rock":
    if computer_choice == "Paper":
        computer_points.append(1)
    elif computer_choice == "Scissor":
        user_points.append(1)
    elif computer_choice == user_input:
        computer_points.append(0)
        user_points.append(0)
elif user_input == "Paper" or "paper":
    if computer_choice == "Rock":
        user_points.append(1)
    elif computer_choice == "Scissor":
        computer_points.append(1)
    elif computer_choice == user_input:
        computer_points.append(0)
        user_points.append(0)
elif user_input == "Scissor" or "scissor":
    if computer_choice == "Paper":
        user_points.append(1)
    elif computer_choice == "Rock":
        computer_points.append(1)
    elif computer_choice == user_input:
        computer_points.append(0)
        user_points.append(0)
round_counter = round_counter + 1
print(user_points) # for debugging
print(computer_points) #for debugging

print("\n")
print("Your score: ", end='')
print(sum(user_points))
print("Computer score: ", end='')
print(sum(computer_points))
print("\n")
if user_points < computer_points:
    print("Sorry, you lost.")
elif user_points > computer_points:
    print("Congratulations, you won!")
else:
    print("You drew with the computer!")
PS94
  • 33
  • 1
  • 1
  • 6
  • `user_input == "Rock" or "rock"` is *always true*, because Python executes that as `(user_input == "Rock") or "rock"`, and the latter, a non-empty string, is considered a true value in a boolean context. – Martijn Pieters Aug 18 '17 at 12:03
  • Python syntax is not English, Python can't and won't guess that you wanted to test `user_input` against two different values. You have to be explicit in computer programming. `user_input in {'Rock', 'rock'}` would work, as would `user_input.lower() == 'rock'` (the latter would also allow `rOcK` and other weird capitalisation combinations). – Martijn Pieters Aug 18 '17 at 12:04
  • Hi Martijn, thanks for the link, much appreciated! – PS94 Aug 18 '17 at 12:06
  • Hi Martijn, yes, I see what I've done now with the user_input == "Rock" or "rock" part, something I didn't catch before. Some handy tips, thank you once again. – PS94 Aug 18 '17 at 12:08

0 Answers0