DEV Community

notyourcuppachai
notyourcuppachai

Posted on

Day 1: Getting the basics down

Things I learnt on Day 1 of coding Python:

  1. Using the "F string" to join text with variables used in the code
  2. How to create functions and how to call functions
  3. Setting up if and elif statements within the code
  4. How to use a dictionary and call elements within the dictionary

Listening to the course I felt like it was easy and I got the basics down, however, when I tried to recreate the game (Of course it was the Rocks, Paper, Scissors game) I was lost in the nitty gritty of it all and struggled in almost every line of how to do certain things.

On my 2nd try yesterday, I did manage to get it right, being a bit slower and taking only a litttlee bit of help from the tutorial.

However, today, when I revisited the game, I was able to recreate almost 91% of the code by myself and only needed a bit of clarification at the end.

So, here is my 91% of the code I recreated again. I know it is basic, but it helps me keep track and learn.

CODE: ROCK, PAPER, SCISSORS

import random

def get_choices():
    player_choice = input("Enter your choice :")
    options = ["rock", "paper", "scissors"]
    computer_choice = random.choice(options)
    choices = {"player": player_choice, "computer": computer_choice}
    return choices


def check_winner(playerchoice, computerchoice):
    print(f"You chose {playerchoice} and computer chose {computerchoice}")

    if playerchoice == computerchoice:
        return "It is TIE"
    elif playerchoice == "rock":
        if computerchoice == "paper":
            return "Paper covers rock. You lose!"
        elif computerchoice == "scissors":
            return "Rock smashes scissors. You win!"
    elif playerchoice == "paper":
        if computerchoice == "rock":
            return "Paper covers rock. You win!"
        elif computerchoice == "scissors":
            return "Paper cut by scissors. You lose!"
    elif playerchoice == "scissors":
        if computerchoice == "paper":
            return "Paper cut by scissors. You win!"
        elif computerchoice == "rock":
            return "Rock smashes scissors. You lose!"


choices = get_choices()
result = check_winner(choices["player"], choices["computer"])
print(result)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)