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)
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.
I cannot thankyou enough! OMG I am so useless lol. got a feeling i’m going to come back to you heh …
I’ve been struggling with this code for hours so thankyou thankyouuuu!!!
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 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