I started reading a book about python two days ago and today I wrote my first code. It's supposed to be a rock, paper, scissors game and from the tests I've done so far it works quite well.
However since I'm new I had to google a lot stuff and I'm 100% sure that there are a lot of things I could have done better, especially regarding the amount of elif-statements that I needed in order to cover every possible outcome.
Any advice is appreciated!
#Imports the random module import random #Prints the introduction to the game print("Welcome! This program allows you to play the famous game: Rock, paper, scissors! \n") #Asks the user for input a = input("Now it's your time to choose, what will you take? Rock, paper or scissors? ") #Repeats the users choice for aesthetic reasons print ("You chose", a, "!") #Turns the users input into a lowercase word for an easier use later on a = a.lower() #Creates a list of choices for the computer to "choose" from game_list = ["Rock", "Paper", "Scissors"] #Lets the computer choose a random option b = random.choice(game_list) #Shows the user which option the computer choose print("The computer chose", b, "!") #Turns the computers choice into a lowercase word for an easier use later on b = b.lower() #All the possible outcomes and conditions the computer has to check if a == "rock" and b == "rock": print("It's a draw!") elif a == "rock" and b == "paper": print("You lost!") elif a == "rock" and b == "scissors": print("You won!") elif a == "paper" and b == "rock": print("You won!") elif a == "paper" and b == "paper": print("It's a draw!") elif a == "paper" and b == "scissors": print("You lost!") elif a == "scissors" and b == "rock": print("You lost!") elif a == "scissors" and b == "paper": print("You won!") elif a == "rock" and b == "scissors": print("It's a draw!") else: print("Something went wrong.") print("Have you typed in your choice correctly?") #The computer says goodbye print("Thank you for playing, I hope you had fun!")
I'll also add my "worksheet" or whatever you want to call it. I've seen a video on Youtube of a guy saying that it's important to first think about what the program has to do, before you start coding.
So here is what I wrote before I started and what I wrote after I was done (corrections) and after I had to add more things that I didn't think of before:
- The program introduces the game to the player.
- The program has to ask the player for input (Rock, paper or scissors).
- It has to save the input as a variable to use it later on.
- The program itself has to choose an option by itself (randomly).
- The program needs to create conditions for outcomes for every possible combination of decisions the player and the computer can make.
- The program has to test the human input and computer choice under the conditions.
- The computer has to tell the player what the outcome is (output).
- The computer should say goodbye.
- I had to import the random module first.
- I decided that it looks better if the program repeats the user input.
- I decided that it was smarter to turn the user input into an lowercase word, so that the program wouldn't run into trouble later on (during the conditions).
- Creating a list made the random choice easier.
- I decided that it looks better if the program outputs the computer choice.
- Lowercase word again.
Advice here is appreciated too, especially regarding my way of writing down what the program has to do.