Rock Paper Scissors Beginner Python Game I recently made this game for rock, paper, scissors in Python 3. Everything works as intended, but I am sure this is not an optimal way of accomplishing this task. Wondering if there is any beginner friendly advice on how to improve upon this. Thank you!

def main():
    import random
    game_active = True
    options = ['Rock', 'Paper', 'Scissors']
    print("\nWelcome to Rock, Paper, Scissors!\n\nPlease choose one:\nR for Rock\nP for Paper\nS for Scissors\n")
    choice = input("Enter your choice: ")
    cpu_choice = random.choice(options)
    if choice != 'R' or 'P' or 'S':
        print("Invalid choice!")
    elif choice == 'R' or 'P' or 'S':
        if choice == 'R':
            choice = "Rock"
        elif choice == 'P':
            choice = "Paper"
        elif choice == 'S':
            choice = "Scissors"

    while choice != cpu_choice:
        if choice == "Rock":
            if cpu_choice == "Paper":
               print("\nYou chose rock and the computer chose paper.\nYou lose!")
            elif cpu_choice == "Scissors":
                print("\nYou chose rock and the computer chose scissors.\nYou win!")
        elif choice == "Paper":
            if cpu_choice == "Rock":
                print("\nYou chose paper and the computer chose rock. \nYou win!")
            elif cpu_choice == "Scissors":
                print("\nYou chose paper and the computer chose scissors. \nYou lose!")
        elif choice == "Scissors":
            if cpu_choice == "Rock":
                print("\nYou chose scissors and the computer chose rock. \nYou lose!")
            elif cpu_choice == "Paper":
                print("\nYou chose scissors and the computer chose paper. \nYou win!")
        break
    while cpu_choice == choice:
        print("You chose", choice + "!")
        print("Computer chooses", cpu_choice + "!")
        break
    cont = input("Play again? (Y/N): ")
    if cont == 'Y':
        main()
    else:
        print("Thank you for playing!")
main()


(This is also my first time using a function)
Welcome to Python!



Choosing Rock/Paper/Scissors/

This

if choice == 'R':
    choice = "Rock"
elif choice == 'P':
    choice = "Paper"
elif choice == 'S':
    choice = "Scissors"


can be

choice = [option for option in options if option.startswith(choice)][0]


Since you're grabbing only the first letter, you can make this comparison, instead of checking each option with its own if statement. This also takes advantage of list comprehension. The array is filled for every value that starts with choice. Since only one value will be in this list, you can simply grab the first value in the list [0].


Utilizing in

This

if choice != 'R' or 'P' or 'S':


can be this

if choice not in "RPS":


This simply checks if the choice is within the string, reducing the need to check each individual character separately.


Unnecessary while

This

while cpu_choice == choice:
    print("You chose", choice + "!")
    print("Computer chooses", cpu_choice + "!")
    break


is basically an if statement, since it's a check and only run once

if cpu_choice == choice:
    print("You chose", choice + "!")
    print("Computer chooses", cpu_choice + "!")


We can make it even neater using string formatting

if cpu_choice == choice:
    print(f"You chose {choice}!")
    print(f"Computer chooses {cpu_choice}!")


Also this

while choice != cpu_choice:


should be this

if choice != cpu_choice:




Imports

Imports should go outside of functions, and at the top of the module

import random

def main():
    ... code ...




Better Input Validation

Right now, you check if the input is valid. If it isn't, you print "Invalid choice!". But you don't stop the program. It keeps running with that choice. You can simplify this by using a while loop here, only breaking if the input is valid:

choice = input("Enter your choice: ")
while choice not in "RPS":
    print("Invalid choice!")
    choice = input("Enter your choice: ")




Unused Variables

This

game_active = True


is never used in your program. You should remove this to avoid confusion, as I initially thought this was the flag that determined if the game was to be run again.