So I wrote this program this evening...it was working just fine and when I started trying to tweak the display I started getting syntax errors on my global elif's. Here's my code, somebody please tell me what is going on.

# Rock Paper Scissors, Project 2, Tadd O'Bannion

import random

def main():
    print("--------------------------------------------------------")
    print(" Welcome to the wonderful world of Paper Rock Scissors! ")
    print("--------------------------------------------------------\n")
    print(" 1) One Player Game ( One human playing against the computer.) \n")
    print(" 2) Two Player Game ( Two humans playing against each other.) \n")
    print("    Choose game type or press ENTER to quit game. \n")
    print("--------------------------------------------")

    menuChoice = input(" Please enter a choice from the above menu: \n--------------------------------------------\n")

    if(menuChoice == "1"):
        print("*****************************************************************")
        print(" You have decided to play a one player game against the computer. \n")
        print("*****************************************************************")
        print(" 1) Paper \n")
        print(" 2) Rock \n")
        print(" 3) Scissors \n")
        print(" 4) Quit \n")

    while(menuChoice != 4):
        subMenuChoice = eval(input( "Please choose from the above weapons menu: \n\n"))

        if( subMenuChoice == 1):
            print(" You have chosen Paper.\n ")
        elif(subMenuChoice == 2):
            print(" You have chosen Rock.\n ")
        elif(subMenuChoice == 3):
            print(" You have chosen Scissors.\n ")
        elif(subMenuChoice == 4):
            print(" You have chosen to quit.\n ")
            return 0
        else:
            print(" Invalid menu choice...seriously...the menu says 1,2,3 or 4...pay attention! \n ")


        computerChoice = random.choice([1, 2, 3])

        if( computerChoice == 1):
            print(" The computer has chosen Paper.\n ")
        elif( computerChoice == 2):
            print(" The computer has chosen Rock.\n ")
        elif( computerChoice == 3):
            print(" The computer has chosen Scissors.\n ")

        if subMenuChoice == computerChoice:
            print(" Tie! ")
        elif (subMenuChoice == 1) and (computerChoice == 2):
            print(" Paper covers Rock!! Human player wins!!\n ")
        elif (subMenuChoice == 2) and (computerChoice == 3):
            print(" Rock smashes Scissors!! Human player wins!!\n ")
        elif (subMenuChoice == 3) and (computerChoice == 1):
            print(" Scissors cuts Paper!! Human player wins!!\n ")
        elif (subMenuChoice == 2) and (computerChoice == 1):
            print(" Paper covers Rock!! Computer wins!!\n ")
        elif (subMenuChoice == 3) and (computerChoice == 2):
            print(" Rock smashes Scissors!! Computer wins!!\n ")
        elif (subMenuChoice == 1) and (computerChoice == 3):
            print(" Scissors cuts Paper!! Computer wins!!\n ")


    elif(menuChoice == "2"):
        print("*****************************************************************")
        print (" You have decided to play a two player game against another human.\n ")
        print("*****************************************************************")
        print(" 1) Paper \n")
        print(" 2) Rock \n")
        print(" 3) Scissors \n")
        print(" 4) Quit \n")

        while (menuChoice != 4):
            player1 = eval(input(" Player 1, please choose from the above weapons menu: \n\n"))
            if( player1 == 1):
                print(" You have chosen Paper.\n ")
            elif(player1 == 2):
                print(" You have chosen Rock.\n ")
            elif(player1 == 3):
                print(" You have chosen Scissors.\n ")
            elif(player1 == 4):
                print(" You have chosen to quit.\n ")
                return 0
            else:
                print(" Invalid menu choice...seriously...the menu says 1,2,3 or 4...pay attention! ")

            player2 = eval(input(" Player 2, please choose from the above weapons menu: \n\n"))
            if( player2 == 1):
                print(" You have chosen Paper.\n ")
            elif(player2 == 2):
                print(" You have chosen Rock.\n ")
            elif(player2 == 3):
                print(" You have chosen Scissors.\n ")
            elif(player2 == 4):
                print(" You have chosen to quit.\n ")
                return 0
            else:
                print(" Invalid menu choice...seriously...the menu says 1,2,3 or 4...pay attention!\n ")

            if player1 == player2:
                print(" Tie! ")
            elif (player1 == 1) and (player2 == 2):
                print(" Paper covers Rock!! Player 1 wins!!!\n ")
            elif (player1 == 2) and (player2 == 3):
                print(" Rock smashes Scissors!! Player 1 wins!!\n ")
            elif (player1 == 3) and (player2 == 1):
                print(" Scissors cuts Paper!! Player 1 wins!!\n ")
            elif (player1 == 2) and (player2 == 1):
                print(" Paper covers Rock!! Player 2 wins!!\n ")
            elif (player1 == 3) and (player2 == 2):
                print(" Rock smashes Scissors!! Player 2 wins!!\n ")
            elif (player1 == 1) and (player2 == 3):
                print(" Scissors cuts Paper!! Player 2 wins!!\n ")
    elif(menuChoice == ""):
        print (" Quitting game. Thank you for playing, please play again soon.\n ")
        return 0
    else:
        print(" Invalid menu choice...seriously...the menu says 1,2 or ENTER...pay attention! \n ")

main()

Please don't tell me how to shorten it, I want this to be a very understandable (from a very new beginner perspective) program so I want to keep it simple.
Thanks in advance.

Dani AI

Generated

Short answer: the SyntaxError on the later elif lines comes from a block-structure/indentation problem. In Python an elif must be directly attached to its if at the same indent level; inserting another top-level compound statement in between (for example a while at the same level) makes the parser see an elif with no matching if, so it flags a syntax error even though the real cause is earlier in the file.

A quick checklist of additional things to look for that aren’t called out explicitly in the thread:

  • Verify the intended block grouping: every compound header (if/elif/else/while/for/def) must be followed by an indented block. If the loop was meant to be inside the if branch, indent the whole loop one level further.
  • Look for mixed tabs and spaces. That often produces confusing IndentationError or TabError; set the editor to insert spaces (PEP8 prefers 4 spaces).
  • Remember that Python’s error marker can point at a later line — check the few lines before the reported error for a missing dedent or extra statement.
  • Watch types from input: in Python 3 input() returns a string, so comparing that menu variable to the integer 4 will never match. Convert and validate user input with int() inside a try/except rather than using eval(), which is risky.
  • Use a basic syntax check (python -m py_compile <file>) or a linter in the editor to get clearer diagnostics.

correctly identified the indentation as the primary fix and offered a useful refactor to reduce repetition; that approach also reduces the chance of the type/logic mistakes above. ’s follow-up confirms the indentation fix solved the parser error.

Recommended Answers

All 4 Replies

The problem is quite simple: your indentation is off. Lines 25-65 need to be one level of indent to the right.

Please don't tell me how to shorten it, I want this to be a very understandable (from a very new beginner perspective) program so I want to keep it simple.

Actually, shortening it in certain places would actually make it easier to understand, beginner or no, just by reducing repetition. For example, just this one function would make the program easier to read while removing several repeated sections of code:

def print_menu():
    print(" 1) Paper")
    print(" 2) Rock")
    print(" 3) Scissors")
    print(" 4) Quit")
    print()

A slightly more elaborate, but even more useful, example would be:

def print_match_results(player1_name, player1_hand, player2_name, player2_hand):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

    outcomes = ['Tie',
                'Paper covers Rock',
                'Rock smashes Scissors',
                'Scissors cuts Paper']

    if player1_hand == ROCK:
        if player2_hand == ROCK:
            print(outcome[0])
        elif SCISSORS:
            print("{0}. {1} wins!".format(outcome[3], player1_name))
        else:
            print("{0}. {1} wins!".format(outcome[1], player2_name))

    if player1_hand == PAPER:
        if player2_hand == PAPER:
            print(outcome[0])
        elif ROCK:
            print("{0}, {1} wins!".format(outcome[1], player1_name))
        else:
            print("{0}, {1} wins!".format(outcome[3], player2_name))

    if player1_hand == SCISSORS:
        if player2_hand == SCISSORS:
            print(outcome[0])
        elif PAPER:
            print("{0}, {1} wins!".format(outcome[3], player1_name))
        else:
            print("{0}, {1} wins!".format(outcome[2], player2_name))            
    print()

At this point, though, we might be pushing things a bit for you, so if you don't quite get it feel free to ignore this.

Oops, I just noticed that I dropped the player2_hand == part of the each of the inner elifs in that second function. My bad.

def print_match_results(player1_name, player1_hand, player2_name, player2_hand):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

    outcomes = ['Tie',
                'Paper covers Rock',
                'Rock smashes Scissors',
                'Scissors cuts Paper']

    outcome_str = '{0}, {1} wins!'

    if player1_hand == ROCK:
        if player2_hand == ROCK:
            print(outcome[0])
        elif player2_hand == SCISSORS:
            print(outcome_str.format(outcome[3], player1_name))
        else:
            print(outcome_str.format(outcome[1], player2_name))

    elif player1_hand == PAPER:
        if player2_hand == PAPER:
            print(outcome[0])
        elif player2_hand == ROCK:
            print(outcome_str.format(outcome[1], player1_name))
        else:
            print(outcome_str.format(outcome[3], player2_name))

    elif player1_hand == SCISSORS:
        if player2_hand == SCISSORS:
            print(outcome[0])
        elif player2_hand == PAPER:
            print(outcome_str.format(outcome[3], player1_name))
        else:
            print(outcome_str.format(outcome[2], player2_name))            
    print()

For what it is worth, here is my full solution, which may or may not make enough sense to you to give you a leg up:

#!/usr/bin/python3
# Rock Paper Scissors, Project 2, Tadd O'Bannion

import sys
import random

ROCK = 0
PAPER = 1
SCISSORS = 2

choices = ['Rock', 'Paper', 'Scissors']

def main():
    print("-" * 50)
    print("Welcome to the wonderful world of Paper Rock Scissors! ")
    print("-" * 50)
    print(" 1) One Player Game ( One human playing against the computer.) \n")
    print(" 2) Two Player Game ( Two humans playing against each other.) \n")
    print("    Choose game type or press ENTER to quit game. \n")
    print("-" * 50)

    menuChoice = input("Please enter a choice from the above menu: ")

    print("*" * 40)
    print('You have decided to ', end='')

    if menuChoice == '':
        print('exit the game. Goodbye.')
        sys.exit()

    elif menuChoice == "1":
        print("play a one player game against the computer.")
        print("*" * 50)

        player1_name = 'Human Player'
        player2_name =  'Computer'
        player2_choice = random.choice([0, 1, 2])

        print_menu()
        player1_choice = read_menu_choice(0, 2)

    else:
        print ("play a two player game against another human.")
        print("*" * 50)
        player1_name = 'Player 1'
        player2_name = 'Player 2'

        print(player1_name)
        print_menu()
        player1_choice = read_menu_choice(0, 2)
        print()
        print(player2_name)
        print_menu()
        player2_choice = read_menu_choice(0, 2)


    print_player_choice(player1_name, player1_choice)
    print_player_choice(player2_name, player2_choice)
    winner = print_match_results(player1_name, player1_choice, player2_name, player2_choice)



def print_menu():
    for n in range(3):
        print(" {0}) {1}".format(n, choices[n]))
    print()


def print_match_results(player1_name, player1_hand, player2_name, player2_hand):
    outcome = ['Tie',
               'Paper covers Rock',
               'Rock smashes Scissors',
               'Scissors cuts Paper']

    combinations = {ROCK:     {ROCK: [0, 0], PAPER: [1, 2], SCISSORS: [2, 1]},
                    PAPER:    {ROCK: [1, 1], PAPER: [0, 0], SCISSORS: [3, 2]},
                    SCISSORS: {ROCK: [2, 2], PAPER: [3, 1], SCISSORS: [0, 0]}}

    print('{0}, '.format(outcome[combinations[player1_hand][player2_hand][0]]), end='')
    if combinations[player1_hand][player2_hand][1] == 0:
        print('no one wins.')
    elif combinations[player1_hand][player2_hand][1] == 1:
        print('{0} wins!'.format(player1_name))
    else:
        print('{0} wins!'.format(player2_name))


def read_menu_choice(lower_choice_range, upper_choice_range):
    choice = 0
    while True:
        try:
            choice = int(input("Please choose from the above menu: "))
            if lower_choice_range <= choice <= upper_choice_range: 
                return choice
            else:
                print("Please choose a valid menu option.")
        except ValueError:
            print("Please choose a valid menu option.")



def print_player_choice(player_name, choice):  
    print('{0} has chosen {1}'.format(player_name, choices[choice]))
    print()


if __name__ == '__main__':
    main()

Thank you a ton! I went out of town today and I apologize for taking so long to respond. I knew it was in the indentation I just couldn't remember where I messed it up. I decided to fix my code, but I will definitely eye-parse through yours to get a feel for some more advanced stuff. I thought Python would be easy since I'm in an intermediate C++ class, but I'm finding out it's like comparing apples to oranges.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.