Day 4 Randomisation and Python Lists

  • Day 4 goals
  • Random Module
  • Lists
    • Appending
    • Offset
  • Coding Exercise: Who will pay the bill?
  • Index Errors and Nested Lists
  • Project: Rock-Paper-Scissors

My “Pay the Bill” exercise

import random
friends = ["Alice", "Bob", "Charlie", "David", "Emanuel"]

# this was my "old fashioned" solution
choice = random.randint(0, 4)
print(friends[choice])

# this was Angela's solution
# print(random.choice(friends))

My Project: Rick-Paper-Scissors

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

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

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

''' 
# the line above was added because the theme does not understand the three single apostrophes. Remove this if you are copying this code.

import random

game_images = [rock, paper, scissors]

# get the users selection
user_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"))
if user_choice >= 0 and user_choice <= 2:
    print("You chose:")
    print(game_images[user_choice])
else:
    print("You typed an invalid number. You lose!")
    exit()

#get the computers choice
computer_choice = random.randint(0, 2)
print("Computer chose:")
print(game_images[computer_choice])

# do the comparison
if user_choice == 0 and computer_choice == 2:
    print("You win!")
elif  computer_choice == 0 and user_choice == 2:
    print("You lose!")
elif user_choice > computer_choice:
    print("You win!")
elif computer_choice > user_choice:
    print("You lose!")
else:
    print("It's a draw!")