100 Days of Code Challenge: Day 4
Oluwatoyin Ariyo
Posted on January 10, 2023
For Day 4 of the 100 Days of Code challenge with Angela Yu's Python Pro bootcamp, I worked with the random module and was introduced to lists and indexes to create a rock paper scissors game that lets the user play against the computer (which takes a random number from 0 to 2) by inputting any number from 0 to 2 where 0 is rock, 1 is paper and 2 is scissors.
Here is the Python code:
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
rps_art = [rock, paper, scissors] #rock is 0 showing rock art, paper is 1 showing and scissors is 2
player_choice = int(input("What do you choose? Type 0 for rock, 1 for paper and 2 for scissors \n"))
print(rps_art[player_choice]) #
computer_choice = random.randint(0, 2)
print("Computer chose: ")
print(rps_art[computer_choice])
if player_choice >= 3 or player_choice < 0:
print("Invalid choice. You lose.")
elif player_choice == 0 and computer_choice == 2: #Player chooses rock and computer chooses scissors
print("Rock beats scissors. You win!")
elif computer_choice > player_choice:
print("You lose!")
elif computer_choice < player_choice:
print("You win!")
elif computer_choice == player_choice:
print("It's a draw!")
I also did the exercise in C# and learnt about the Random class and how it is used. Here is the C# code:
string rock = "Rock";
string paper = "Paper";
string scissors = "Scissors";
string[] rps_art = { rock, paper, scissors };
Console.WriteLine("What do you choose? Type 0 for rock, type 1 for paper or type 2 for scissors");
int player_choice = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(rps_art[player_choice]);
Random random = new Random();
int computer_choice = random.Next(0, 2);
Console.WriteLine("Computer chose: ");
Console.WriteLine(rps_art[computer_choice]);
if (player_choice >= 3 || computer_choice < 0)
{
Console.WriteLine("Invalid choice. You lose");
} else if (player_choice == 0 && computer_choice == 2)
{
Console.WriteLine("Rock beats scissors. You win!");
} else if (computer_choice > player_choice)
{
Console.WriteLine("You lose!");
} else if (computer_choice < player_choice)
{
Console.WriteLine("You win!");
} else if (computer_choice == player_choice)
{
Console.WriteLine("It's a draw");
}
I really enjoyed doing this project and learning about how modules work in Python. Tomorrow, I will do day 5 of the project where I will learn about for loops and develop a project around it.
Posted on January 10, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.