Expected an indented block after 'elif' statement

Hello. This is my first post, so please go easy on a 13year old. It’s my absolute first time I’m trying to learn python. I’ve been trying to create a small rock, papers, scissors program, but I keep getting “expected an indented block after ‘elif’ statement”.

Could you please help me?

Here is the code I’ve typed so far:-

import random

def get_choices():
  player_choice = input("Enter a choice (rock, paper, scissors: ")
  options = ["rock", "paper", "scissors"]
  computer_choice = random.choice(options)
  choices = {"player": player_choice, "computer": computer_choice}
  return choices

def check_win(player, computer):
    print(f"you chose {player}, computer chose {computer}")
    if player == computer:
        return "It's a tie!"

 elif player == "rock":
 if computer == "scissors":
        return "rock smashes scissors! You win!"
      else:
     return "Rock smashes scissors! You win!"
 elif player == "paper":
 if computer == "rock":
        return "paper covers rock! You lose!"
      else:
     return "Scissors cuts paper! You lose."
 elif player == "scissors":
 if computer == "paper":
        return "scissors cuts paper! You win!"
      else:
     return "Rock smashes scissors! You lose."

choices = get_choices()

result = check_win(choices["player"], choices["computer"])
print(result)

Welcome to our community!

Indentation plays a very important role in Python. You should pay attention to this fact. Your code should look like the following:

if player == computer:
        return "It's a tie!"

    elif player == "rock":
        if computer == "scissors":
            return "rock smashes scissors! You win!"
        else:
            return "Rock smashes scissors! You win!"
    elif player == "paper":
        if computer == "rock":
            return "paper covers rock! You lose!"
        else:
            return "Scissors cuts paper! You lose." etc.

As you can see, the order must be followed when it comes to indentation.

Now, the program works fine:

1 Like

I cannot thankyou enough! OMG I am so useless lol. got a feeling i’m going to come back to you heh :sweat_smile:
I’ve been struggling with this code for hours so thankyou thankyouuuu!!! :sparkling_heart: :sparkling_heart: :sparkling_heart:

1 Like

Hello, beginner python programmer here.
I’ve made a basic rock, paper scissors program. However i’m wondering how I make the program loop infinitely.
Here is the program:

import random

def get_choices():
  player_choice = input("Enter a choice (rock, paper, scissors: ")
  options = ["rock", "paper", "scissors"]
  computer_choice = random.choice(options)
  choices = {"player": player_choice, "computer": computer_choice}
  return choices

def check_win(player, computer):
        print(f"you chose {player}, computer chose {computer}")
        if player == computer:
            return "It's a tie!"

        elif player == "rock":
            if computer == "scissors":
                return "rock smashes scissors! You win!"
            else:
                return "Rock smashes scissors! You win!"
        elif player == "paper":
            if computer == "rock":
                return "paper covers rock! You lose!"
            else:
                return "Scissors cuts paper! You lose."
        elif player == "scissors":
            if computer == "paper":
                return "scissors cuts paper! You win!"
            else:
                return "rock smashes scissors! You lose."

choices = get_choices()

result = check_win(choices["player"], choices["computer"])
print(result)

You could call check_win at the end of check_win

Hi, umm may I ask how I do that??? :thinking: :hugs:
sorry I’m quite the beginner hehe :sweat_smile:

You can place your main program inside a while loop to make it loop endlessly:

while True:
    #main program here

But this loop will never stop until you press Ctrl-C. If you want the user to decide whether to continue or quit after each game, you can add input statement after the main program to get user’s decision, then use a if statement to break the loop. For example:

while True:
    #main program here
    answer = input("One more game? Enter Q for quit") 
    if answer == "Q":
        break

Another way is to set a flag at the beginning and use it as the condition for the while loop, then ask and change the status of the flag after the main program and stop iterating:

roll = True
while roll:
    #main program here
    answer = input("One more game? Enter Q for quit") 
    if answer == "Q":
        roll = False

This is the approach I recommend

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