Not sure what's wrong with my code :o (Rock, Paper Scissor Project)


#1

Terminal keeps saying there’s something wrong with line 20, “SyntaxError: invalid syntax”.

I spent almost an hour trying to figure out what’s wrong with my code. Could it be a bug? Or maybe something’s wrong with my browser?

UPDATE: I just looked at my own code on Github, it shows on the site that some of the “elif” statements are wrongly indented, however, I assure you every thing is properly formatted on the code editor that I was working on. I guess maybe it’s a bug? -_-


#2

bug in your code.

looking at this part of your code:

  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"])

your if, first elif and else are indented fine. Why the second and third elif are not properly indented is a mystery to me. You seem to understand the concept fine :slight_smile:


#3

This is what I see on my code editor T_T


#4

the editor uses hard tabs, sometimes that causes indention problems although it looks fine.

Just re-indent around those lines

I would just set up a text-editor (like atom) with soft tab, and let the text-editor re-indent the whole things


#5

Ah. It works now. Thanks! :slight_smile:


#6

here is the code with soft tab (4 spaces, according to pep8 recommendations):

"""This is a 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: " + user_choice)
    print("The Computer selected: " + 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 Scissor: ")
    user_choice = user_choice.upper()
    computer_choice = options[randit(0, 2)]
    decide_winner(user_choice, computer_choice)

play_RPS()