#๐Ÿ”’ Beginner Python Project: Is My Rock Paper Scissors Code Efficient?

28 messages ยท Page 1 of 1 (latest)

fossil prairie
#

I'm new to Python and recently coded a rock-paper-scissors game as a learning exercise. My goal was to get more familiar with the random function and conditional statements. I'd love to get some feedback on my code โ€“ are there ways I could make it more efficient or improve its readability?

Here's the code:
`import random

paper_opt = "Paper"
scissors_opt = "Scissors"
rock_opt = "Rock"
options = [paper_opt, scissors_opt, rock_opt]

while True:
user_choice = input(f"please pick between {paper_opt}. {scissors_opt} or {rock_opt} :")

if user_choice.lower() == paper_opt.lower():
    print(f"You played {paper_opt}")
    break
elif user_choice.lower() == scissors_opt.lower():
    print(f"You played {scissors_opt}")
    break
elif user_choice.lower() == rock_opt.lower():
    print(f"You played {rock_opt}")
    break
else:
    print("Invalid choice. please pick Paper, Scissors or Rock")

opponent_choice = random.choice(options)
print(f"The opponent played {opponent_choice}")

if user_choice.lower() == opponent_choice.lower():
print("It's a tie!")
elif (user_choice.lower() == "paper") and opponent_choice.lower() == "rock" or
(user_choice.lower() == "rock") and opponent_choice.lower() == "scissors" or
(user_choice.lower() == "scissors") and opponent_choice.lower() == "paper":
print("You Win!")
else:
print("You Lose!")`

gusty tartanBOT
#

@fossil prairie

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hallow quarry
#
if user_choice.lower() == paper_opt.lower():
        print(f"You played {paper_opt}")
        break
    elif user_choice.lower() == scissors_opt.lower():
        print(f"You played {scissors_opt}")
        break
    elif user_choice.lower() == rock_opt.lower():
        print(f"You played {rock_opt}")
        break
    else:
        print("Invalid choice. please pick Paper, Scissors or Rock")
#

this is quite ineffecient

high smelt
hallow quarry
#

you already know what the user played because it's stored in the variable already

#

you can simply check if it's a valid option, and if so, display it

#
if user_choice in options:
    print(f"You picked {user_choice}")
lapis path
#

I think there might be a reason for the .lower() so that the options are not case sensitive

hallow quarry
#

yes, but they're saying to apply lower() immediately so you don't have to use .lower() every single time you refer to that variable

fossil prairie
#

thanks for the advice!

lapis path
#

imo at this level efficiency isn't as important as readability

fossil prairie
#

yeah I get that, I suppose I was more looking at as trying to avoid bad habits

lapis path
#

.capitalize could be a good idea

#

!e

print("heLLo wOrLD".capitalize())
gusty tartanBOT
#

@lapis path :white_check_mark: Your 3.12 eval job has completed with return code 0.

Hello world
lapis path
#
import random

paper_opt = "Paper"
scissors_opt = "Scissors"
rock_opt = "Rock"
options = [paper_opt, scissors_opt, rock_opt]

while True:
    user_choice = input(f"please pick between {paper_opt}. {scissors_opt} or {rock_opt} :").capitalize()
    if user_choice in options:
        print(f"You played {user_choice}")
        break
    else:
        print("Invalid choice. please pick Paper, Scissors or Rock")

opponent_choice = random.choice(options)
print(f"The opponent played {opponent_choice}")

if user_choice == opponent_choice:
    print("It's a tie!")
elif (options.index(user_choice) - options.index(opponent_choice)) % 3 == 1: # magic??
    print("You Win!")
else:
    print("You Lose!")
fossil prairie
#

... wow i need to figure out how that index function works hahaha

hallow quarry
#

since the list of options are in order, you can use the index positions to figure out if the player options are in some sort of order

#

index 1 beats index 0

#

index 2 beats index 1

#

index 0 beats index 2

fossil prairie
#

oh cool, yeah i get it. thats neat

keen mauve
#

to make it a tiny bit better i would make a random for the indices and then whenever i need to print the value, i get them from the list
that way all the conditions still work and i don't have to use options.index at the end

fossil prairie
#

so... like
opponent_index = random.randint(0, 2) opponent_choice = options[opponent_index]

and

elif (options.index(user_choice) - opponent_index) % 3 == 1:

lapis path
#

yeah that can be done too

gusty tartanBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.