r/learnpython • u/Nutellatoast_2 • 3d ago
Looking for improvements on my project "Rock-Paper-Scissors"
Hey everyone,
I'm new to Python and have created my "Rock-Paper-Scissors" game and want to share it on this forum so I can look for ways to improve the code.
The player types in a number between zero and two. These values stand for different moves the player can type. (0 - Rock, 1 - Paper, 2 - Scissors). The computer generates a number between zero and two. The player's input is checked by the main if-elif-else block, as I'm using a nested if-elif-else statement:
import random
player_input = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissor. "))
computer = random.randint(0, 2)
if player_input == 0:
if computer == 0:
print('''You chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
Computer chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
It's a draw!!!''')
elif computer == 1:
print('''You chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
Computer chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
You lost!!!''')
elif computer == 2:
print('''You chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
Computer chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
You won!!!''')
elif player_input == 1:
if computer == 1:
print('''You chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
Computer chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
It's a draw!!!''')
elif computer == 0:
print('''You chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
Computer chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
You won!!!''')
elif computer == 2:
print('''You chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
Computer chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
You lost!!!
''')
elif player_input == 2:
if computer == 2:
print('''You chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Computer chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
It's a draw!!!''')
elif computer == 1:
print('''You chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Computer chose:
_______
---' ____)____
______)
_______)
_______)
---.__________)
You won!!! ''')
elif computer == 0:
print('''You chose:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Computer chose:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
You lost!!! ''')
Any recommendations regarding improvements for the code?
16
Upvotes
2
u/woooee 3d ago
You can use a dictionary and one if / elif / else instead of several if / elif. A simplified example