Kyle Leonard
Black Nerd's Journey

Black Nerd's Journey

Day 4: Randomization and Python Lists

Day 4: Randomization and Python Lists

Kyle Leonard's photo
Kyle Leonard
ยทAug 14, 2022ยท

3 min read

Today's Lesson

I feel like Day 4 was a much need breather from day 3. The assignments and video lectures seem to click a bit more. The main takeaway was the use of the Random Module and Lists.

Random Module

The random module is a built-in module to help you generate random elements. It was pretty straight forward and the assignments were better explained as well, making them quicker to work through. An example of that is the Heads or Tails assignment below:

#Remember to use the random module
#Hint: Remember to import the random module here at the top of the file. ๐ŸŽฒ
import random
# ๐Ÿšจ Don't change the code below ๐Ÿ‘‡
test_seed = int(input("Create a seed number: "))
random.seed(test_seed)
 # ๐Ÿšจ Don't change the code above ๐Ÿ‘† It's only for testing your code.

#Write the rest of your code below this line ๐Ÿ‘‡

random_int = random.randint(0, 1)

if random_int == 1:
    print("Heads")
else:
    print("Tails")

Lists

Lists in Python are pretty much like Arrays in JS. We went over indexing, index errors, and nesting lists. The same thing goes with the Lists assignments as in they were clearly explained. Example assignment below:

# ๐Ÿšจ Don't change the code below ๐Ÿ‘‡
row1 = ["โฌœ๏ธ","โฌœ๏ธ","โฌœ๏ธ"]
row2 = ["โฌœ๏ธ","โฌœ๏ธ","โฌœ๏ธ"]
row3 = ["โฌœ๏ธ","โฌœ๏ธ","โฌœ๏ธ"]
map = [row1, row2, row3]
print(f"{row1}\n{row2}\n{row3}")
position = input("Where do you want to put the treasure?")
# ๐Ÿšจ Don't change the code above ๐Ÿ‘†

#Write your code below this row ๐Ÿ‘‡
horizontal = int(position[0])
vertical = int(position[1])


selected_row = (map[vertical - 1])
selected_row[horizontal - 1] = "X"

#Write your code above this row ๐Ÿ‘†

# ๐Ÿšจ Don't change the code below ๐Ÿ‘‡
print(f"{row1}\n{row2}\n{row3}")

Day 4 Project - Rock Paper Scissors

The Rock Paper Scissors game I feel was pretty easy to get done once you figure out the logic. Basically you start off with the user input ranging from 0-2 and then you get the computer input by using the random generator. After the inputs are stored and changed to integers, you just go through a bunch of if/elif statements to compare the values and see who won. My code can be found below:

import random
rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

# Write your code below this line ๐Ÿ‘‡

user_choice = int(input(
    "What do you choose? Type 0 for Rock, 1 for Paper, 2 for Scissors:\n "))

computer_choice = random.randint(0, 2)
print(f"Computer chose {computer_choice}")

if user_choice == computer_choice:
    print("It's a tie!")
elif user_choice == 0 and computer_choice == 1:
    print("Computer wins!")
elif user_choice == 0 and computer_choice == 2:
    print("User wins!")
elif user_choice == 1 and computer_choice == 0:
    print("User wins!")
elif user_choice == 1 and computer_choice == 2:
    print("Computer wins!")
elif user_choice == 2 and computer_choice == 0:
    print("Computer wins!")
elif user_choice == 2 and computer_choice == 1:
    print("User wins!")
else:
    print("Invalid Input, loser")

##EOD I feel today was needed after going through a rough day 3 ๐Ÿ˜…. I also know I said I was going to do day 4 AND day 5 but I needed to do some dog sitting for a bit and let's be honest, day 5 is going to be about loops and that always tripped me up in JS and I still don't have quite the handle on it lol. So I'll double back on that tomorrow.

Goodnight folks โœŒ๐Ÿพ

Did you find this article valuable?

Support Kyle Leonard by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this