# Day 017 - Project 17 : Rock Paper Scissors Multiple Rounds

I did this lesson only using while and it worked well. Is it a problem if I don’t use break, continue and exit?

``````from getpass import getpass as input

print("E P I C    🪨  📄 ✂️    B A T T L E ")
countPlayer1 = 0
countPlayer2 = 0

while True:

print("Select your move (R, P or S)")
print()
player1Move = input("Player 1 > ")
player2Move = input("Player 2 > ")

if player1Move == "R" and player2Move == "R" or player1Move == "P" and player2Move == "P" or player1Move == "S" and player2Move == "S":
print("Draw! No points awarded.")
continue
elif player1Move == "R" and player2Move == "P"or player1Move == "P" and player2Move == "S":
print("In this round, Player 2 wins!")
countPlayer2 += 1
if countPlayer1 == 3:
print ('Player 1 has', countPlayer1, 'points and wins. And Player 2 has', countPlayer2, 'wins.')
exit()
elif countPlayer2 == 3:
print ('Player 2 has', countPlayer2, 'points and wins. And Player 1 has', countPlayer1, 'wins.')
exit()
else:
continue
elif player1Move == "R" and player2Move == "S"or player1Move == "P" and player2Move == "R":
print("In this round, Player 1 wins!")
countPlayer1 += 1
if countPlayer1 == 3:
print ('Player 1 has', countPlayer1, 'wins. And Player 2 has', countPlayer2, 'wins.')
exit()
elif countPlayer2 == 3:
print ('Player 2 has', countPlayer2, 'wins. And Player 1 has', countPlayer1, 'wins.')
exit()
else:
continue
else:
print("Try Again, invalid entry!")
continue

``````

Hey Everyone,
This is how I did my project, and it works, but I do feel like we could achieve similar results with fewer lines of code. Does anyone have any suggestions for how to improve this? Thank you in advance.

Hey @rkhattab!

Well, for one, to reduce the number of lines of code, you can remove duplicate checks if the player wins.

I mean this:

``````  if player1Move == "R" and player2Move == "R" or player1Move == "P" and player2Move == "P" or player1Move == "S" and player2Move == "S":
print("Draw! No points awarded.")
continue
elif player1Move == "R" and player2Move == "P"or player1Move == "P" and player2Move == "S":
print("In this round, Player 2 wins!")
countPlayer2 += 1
elif player1Move == "R" and player2Move == "S"or player1Move == "P" and player2Move == "R":
print("In this round, Player 1 wins!")
countPlayer1 += 1
else:
print("Try Again, invalid entry!")
if countPlayer1 == 3:
print ('Player 1 has', countPlayer1, 'wins. And Player 2 has', countPlayer2, 'wins.')
exit()
elif countPlayer2 == 3:
print ('Player 2 has', countPlayer2, 'wins. And Player 1 has', countPlayer1, 'wins.')
exit()
``````
2 Likes

Hey @rkhattab!

Adding on to what @QwertyQwerty88 said, I believe you can shorten it a bit more by removing the space between lines (this does remove the readability). You can also, for the `else:` line put it’s running code on the same line as it (the `else:`). Like so →

``````from getpass import getpass as input
print("E P I C    🪨  📄 ✂️    B A T T L E ")
countPlayer1 = 0
countPlayer2 = 0
while True:
print("Select your move (R, P or S)")
print()
player1Move = input("Player 1 > ")
player2Move = input("Player 2 > ")
if player1Move == "R" and player2Move == "R" or player1Move == "P" and player2Move == "P" or player1Move == "S" and player2Move == "S":
print("Draw! No points awarded.")
continue
elif player1Move == "R" and player2Move == "P"or player1Move == "P" and player2Move == "S":
print("In this round, Player 2 wins!")
countPlayer2 += 1
elif player1Move == "R" and player2Move == "S"or player1Move == "P" and player2Move == "R":
print("In this round, Player 1 wins!")
countPlayer1 += 1
else: print("Try Again, invalid entry!")
if countPlayer1 == 3:
print ('Player 1 has', countPlayer1, 'wins. And Player 2 has', countPlayer2, 'wins.')
exit()
elif countPlayer2 == 3:
print ('Player 2 has', countPlayer2, 'wins. And Player 1 has', countPlayer1, 'wins.')
exit()
``````

This does reduce it down to 25 lines of code, but it is harder to read without the spaces between the lines. I hope this helps!

1 Like

Yeah, this is great. I was wondering you know how we have the if condition where I use the ‘or’ statement. If I wanted to print “Scissors beats paper” or "paper beats rock’ based on the input do I need to create an if loop inside the elif block? Or how can I add what happened in that round so users can see what player 1 and player 2 selected?

Hope my question makes sense. Thanks!

You could make a dictionary, like this:

``````moves = {
"r": "rock",
"p": "paper",
"s": "scissors"
}
``````

Then, when you’re printing:

``````elif player1Move == "R" and player2Move == "P"or player1Move == "P" and player2Move == "S":
print(f"{moves[player2Move.lower()].title()} beats {moves[player1Move.lower()]}, Player 2 wins!")
countPlayer2 += 1
``````

And do the same thing for when player 1 wins.

2 Likes

Alright, this is great, I just haven’t reached the dictionaries lesson yet. Thanks.

1 Like

The example code from Project 17 has a line (“game is over”) that never runs at the end after exit(). Why did they put it there?
Is it a bug?

``````while True:
print("You are in a corridor, do you go left or right?")
direction = input("> ")
if direction == "left":
print("You have fallen to your death")
break
elif direction == "right":
continue
else:
print("Ahh! You're a genius, you've won")
exit()
print("The game is over, you've failed!")
``````

When the tutorials were made, the bug with `exit()` did not occur.

2 Likes

The game over line is only supposed to run when you choose left.

It won’t run after `exit()` because when you use `exit()` the program ends right then and nothing after it will be ran. So for your question why it wont run after exit() is because 1 nothing will be ran after exit(), and also because you are still inside the loop and the game over line is outside of the loop.

When you choose right you `continue` the loop, asking which direction to take again.

When you choose left you `break` out of the while True loop, allowing you to run the rest of the code outside of the loop, which in this case is just the game over line.

I think they put it there to help show you the difference between `break` and `continue`.

In the video starting at about the 47 second mark, David explains that if you choose left, the break command kicks you out of the loop to the game over line because you’ve “fallen to your death”.

1 Like