5.9 Homework and popcorn hacks
5.9 Homework and popcorn hacks
# Popcorn Hack Number 2 (Random): Make a random algorithm to choose a daily activity:
import random
# Step 1: Define a list of activities
activities = ['[play soccer]', 'read a book', 'go for a walk', 'sleep', 'Watch a movie', 'eat food', 'Go for a run', 'Write in a journal', 'Meditate', 'Listen to music', ]
# Step 2: Randomly choose an activity
random_activity = random.choice(activities)
# Step 3: Display the chosen activity
print(f"Today’s random activity: {random_activity}")
Today’s random activity: read a book
# Popcorn Hack Number 3: Using a loops in random
# This popcorn hack assigns an activity to each person
import random
hosts = ['Hannah', 'Shriya', 'Gaheera']
activities = ['code code code', 'read', 'snack', 'photo booth', 'beach']
# Randomly shuffle the list of activities to assign them randomly to the guests
random.shuffle(activities)
# Loop through each guest and assign them a random activity
for i in range(len(hosts)):
print(f"{hosts[i]} will be monitoring {activities[i]}!")
Hannah will be monitoring snack!
Shriya will be monitoring beach!
Gaheera will be monitoring code code code!
import random
def spin_number():
return random.randint(1, 20)
spinner_result = spin_number()
print("Spinner Result:", spinner_result)
Spinner Result: 7
import random
def play_rock_paper_scissors():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("Enter your choice (rock, paper, or scissors): ")
if user_choice not in choices:
print("Invalid choice. Please try again.")
return
print("Computer chose:", computer_choice)
print("You chose:", user_choice)
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'paper' and computer_choice == 'rock') or \
(user_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
else:
print("You lose!")
play_rock_paper_scissors()
Computer chose: rock
You chose: paper
You win!