Solutions - Chapter 9
- 9-1: Restaurant
- 9-2: Three Restaurants
- 9-3: Users
- 9-4: Number Served
- 9-5: Login Attempts
- 9-6: Ice Cream Stand
- 9-7: Admin
- 9-8: Privileges
- 9-9: Battery Upgrade
- 9-10: Imported Restaurant
- 9-11: Imported Admin
- 9-12: Multiple Modules
- 9-13: Dice
- 9-14: Lottery
- 9-15: Lottery Analysis
Back to solutions.
9-1: Restaurant
Make a class called Restaurant
. The __init__()
method for Restaurant
should store two attributes: a restaurant_name
and a cuisine_type
. Make a method called describe_restaurant()
that prints these two pieces of information, and a method called open_restaurant()
that prints a message indicating that the restaurant is open.
Make an instance called restaurant
from your class. Print the two attributes individually, and then call both methods.
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = f"{self.name} serves wonderful {self.cuisine_type}."
print(f"\n{msg}")
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = f"{self.name} is open. Come on in!"
print(f"\n{msg}")
restaurant = Restaurant('the mean queen', 'pizza')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
Output:
The Mean Queen
pizza
The Mean Queen serves wonderful pizza.
The Mean Queen is open. Come on in!
9-2: Three Restaurants
Start with your class from Exercise 9-1. Create three different instances from the class, and call describe_restaurant()
for each instance.
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = f"{self.name} serves wonderful {self.cuisine_type}."
print(f"\n{msg}")
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = f"{self.name} is open. Come on in!"
print(f"\n{msg}")
mean_queen = Restaurant('the mean queen', 'pizza')
mean_queen.describe_restaurant()
ludvigs = Restaurant("ludvig's bistro", 'seafood')
ludvigs.describe_restaurant()
mango_thai = Restaurant('mango thai', 'thai food')
mango_thai.describe_restaurant()
Output:
The Mean Queen serves wonderful pizza.
Ludvig'S Bistro serves wonderful seafood.
Mango Thai serves wonderful thai food.
9-3: Users
Make a class called User
. Create two attributes called first_name
and last_name
, and then create several other attributes that are typically stored in a user profile. Make a method called describe_user()
that prints a summary of the user’s information. Make another method called greet_user()
that prints a personalized greeting to the user.
Create several instances representing different users, and call both methods for each user.
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
eric = User('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.greet_user()
willie = User('willie', 'burger', 'willieburger', 'wb@example.com', 'alaska')
willie.describe_user()
willie.greet_user()
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
Welcome back, e_matthes!
Willie Burger
Username: willieburger
Email: wb@example.com
Location: Alaska
Welcome back, willieburger!
9-4: Number Served
Start with your program from Exercise 9-1 (page 166). Add an attribute called number_served
with a default value of 0. Create an instance called restaurant
from this class. Print the number of customers the restaurant has served, and then change this value and print it again.
Add a method called set_number_served()
that lets you set the number of customers that have been served. Call this method with a new number and print the value again.
Add a method called increment_number_served()
that lets you increment the number of customers who’ve been served. Call this method with any number you like that could represent how many customers were served in, say, a day of business.
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = f"{self.name} serves wonderful {self.cuisine_type}."
print(f"\n{msg}")
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = f"{self.name} is open. Come on in!"
print(f"\n{msg}")
def set_number_served(self, number_served):
"""Allow user to set the number of customers that have been served."""
self.number_served = number_served
def increment_number_served(self, additional_served):
"""Allow user to increment the number of customers served."""
self.number_served += additional_served
restaurant = Restaurant('the mean queen', 'pizza')
restaurant.describe_restaurant()
print(f"\nNumber served: {restaurant.number_served}")
restaurant.number_served = 430
print(f"Number served: {restaurant.number_served}")
restaurant.set_number_served(1257)
print(f"Number served: {restaurant.number_served}")
restaurant.increment_number_served(239)
print(f"Number served: {restaurant.number_served}")
Output:
The Mean Queen serves wonderful pizza.
Number served: 0
Number served: 430
Number served: 1257
Number served: 1496
9-5: Login Attempts
Add an attribute called login_attempts
to your User
class from Exercise 9-3 (page 166). Write a method called increment_login_attempts()
that increments the value of login_attempts
by 1. Write another method called reset_login_attempts()
that resets the value of login_attempts
to 0.
Make an instance of the User
class and call increment_login_attempts()
several times. Print the value of login_attempts
to make sure it was incremented properly, and then call reset_login_attempts()
. Print login_attempts
again to make sure it was reset to 0.
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
eric = User('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.greet_user()
print("\nMaking 3 login attempts...")
eric.increment_login_attempts()
eric.increment_login_attempts()
eric.increment_login_attempts()
print(f" Login attempts: {eric.login_attempts}")
print("Resetting login attempts...")
eric.reset_login_attempts()
print(f" Login attempts: {eric.login_attempts}")
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
Welcome back, e_matthes!
Making 3 login attempts...
Login attempts: 3
Resetting login attempts...
Login attempts: 0
9-6: Ice Cream Stand
An ice cream stand is a specific kind of restaurant. Write a class called IceCreamStand
that inherits from the Restaurant
class you wrote in Exercise 9-1 (page 166) or Exercise 9-4 (page 171). Either version of the class will work; just pick the one you like better. Add an attribute called flavors
that stores a list of ice cream flavors. Write a method that displays these flavors. Create an instance of IceCreamStand
, and call this method.
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = f"{self.name} serves wonderful {self.cuisine_type}."
print(f"\n{msg}")
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = f"{self.name} is open. Come on in!"
print(f"\n{msg}")
def set_number_served(self, number_served):
"""Allow user to set the number of customers that have been served."""
self.number_served = number_served
def increment_number_served(self, additional_served):
"""Allow user to increment the number of customers served."""
self.number_served += additional_served
class IceCreamStand(Restaurant):
"""Represent an ice cream stand."""
def __init__(self, name, cuisine_type='ice_cream'):
"""Initialize an ice cream stand."""
super().__init__(name, cuisine_type)
self.flavors = []
def show_flavors(self):
"""Display the flavors available."""
print("\nWe have the following flavors available:")
for flavor in self.flavors:
print(f"- {flavor.title()}")
big_one = IceCreamStand('The Big One')
big_one.flavors = ['vanilla', 'chocolate', 'black cherry']
big_one.describe_restaurant()
big_one.show_flavors()
Output:
The Big One serves wonderful ice_cream.
We have the following flavors available:
- Vanilla
- Chocolate
- Black Cherry
9-7: Admin
An administrator is a special kind of user. Write a class called Admin
that inherits from the User
class you wrote in Exercise 9-3 (page 166) or Exercise 9-5 (page 171). Add an attribute, privileges
, that stores a list of strings like "can add post"
, "can delete post"
, "can ban user"
, and so on. WRite a method called show_privileges()
that lists the administrator’s set of privileges. Create an instance of Admin
, and call your method.
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
class Admin(User):
"""A user with administrative privileges."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the admin."""
super().__init__(first_name, last_name, username, email, location)
self.privileges = []
def show_privileges(self):
"""Display the privileges this administrator has."""
print("\nPrivileges:")
for privilege in self.privileges:
print(f"- {privilege}")
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.privileges = [
'can reset passwords',
'can moderate discussions',
'can suspend accounts',
]
eric.show_privileges()
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
Privileges:
- can reset passwords
- can moderate discussions
- can suspend accounts
9-8: Privileges
Write a separate Privileges
class. The class should have one attribute, privileges
, that stores a list of strings as described in Exercise 9-7. Move the show_privileges()
method to this class. Make a Privileges
instance as an attribute in the Admin
class. Create a new instance of Admin
and use your method to show its privileges.
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
class Admin(User):
"""A user with administrative privileges."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the admin."""
super().__init__(first_name, last_name, username, email, location)
# Initialize an empty set of privileges.
self.privileges = Privileges()
class Privileges():
"""A class to store an admin's privileges."""
def __init__(self, privileges=[]):
self.privileges = privileges
def show_privileges(self):
print("\nPrivileges:")
if self.privileges:
for privilege in self.privileges:
print(f"- {privilege}")
else:
print("- This user has no privileges.")
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.privileges.show_privileges()
print("\nAdding privileges...")
eric_privileges = [
'can reset passwords',
'can moderate discussions',
'can suspend accounts',
]
eric.privileges.privileges = eric_privileges
eric.privileges.show_privileges()
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
Privileges:
- This user has no privileges.
Adding privileges...
Privileges:
- can reset passwords
- can moderate discussions
- can suspend accounts
9-9: Battery Upgrade
Use the final version of electric_car.py from this section. Add a method to the Battery
class called upgrade_battery()
. This method should check the battery size and set the capacity to 100 if it isn’t already. Make an electric car with a default battery size, call get_range()
once, and then call get_range()
a second time after upgrading the battery. You should see an increase in the car’s range.
class Car():
"""A simple attempt to represent a car."""
def __init__(self, manufacturer, model, year):
"""Initialize attributes to describe a car."""
self.manufacturer = manufacturer
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.year} {self.manufacturer} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""
Set the odometer reading to the given value.
Reject the change if it attempts to roll the odometer back.
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
class Battery():
"""A simple attempt to model a battery for an electric car."""
def __init__(self, battery_size=75):
"""Initialize the batteery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 75:
range = 260
elif self.battery_size == 100:
range = 315
message = f"This car can go approximately {range}"
message += " miles on a full charge."
print(message)
def upgrade_battery(self):
"""Upgrade the battery if possible."""
if self.battery_size == 75:
self.battery_size = 100
print("Upgraded the battery to 100 kWh.")
else:
print("The battery is already upgraded.")
class ElectricCar(Car):
"""Models aspects of a car, specific to electric vehicles."""
def __init__(self, manufacturer, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(manufacturer, model, year)
self.battery = Battery()
print("Make an electric car, and check the range:")
my_tesla = ElectricCar('tesla', 'roadster', 2019)
my_tesla.battery.get_range()
print("\nUpgrade the battery, and check the range again:")
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()
Output:
Make an electric car, and check the range:
This car can go approximately 260 miles on a full charge.
Upgrade the battery, and check the range again:
Upgraded the battery to 100 kWh.
This car can go approximately 315 miles on a full charge.
9-10: Imported Restaurant
Using your latest Restaurant
class, store it in a module. Make a separate file that imports Restaurant
. Make a Restaurant
instance, and call one of Restaurant
’s methods to show that the import
statement is working properly.
restaurant.py:
"""A class representing a restaurant."""
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = f"{self.name} serves wonderful {self.cuisine_type}."
print(f"\n{msg}")
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = f"{self.name} is open. Come on in!"
print(f"\n{msg}")
def set_number_served(self, number_served):
"""Allow user to set the number of customers that have been served."""
self.number_served = number_served
def increment_number_served(self, additional_served):
"""Allow user to increment the number of customers served."""
self.number_served += additional_served
my_restaurant.py:
from restaurant import Restaurant
channel_club = Restaurant('the channel club', 'steak and seafood')
channel_club.describe_restaurant()
channel_club.open_restaurant()
Output:
The Channel Club serves wonderful steak and seafood.
The Channel Club is open. Come on in!
9-11: Imported Admin
Start with your work from Exercise 9-8 (page 173). Store the classes User
, Privileges
and Admin
in one module. Create a separate file, make an Admin
instance, and call show_priveleges()
to show that everything is working correctly.
user.py:
"""A collection of classes for modeling users."""
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
class Admin(User):
"""A user with administrative privileges."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the admin."""
super().__init__(first_name, last_name, username, email, location)
# Initialize an empty set of privileges.
self.privileges = Privileges()
class Privileges():
"""A class to store an admin's privileges."""
def __init__(self, privileges=[]):
self.privileges = privileges
def show_privileges(self):
print("\nPrivileges:")
if self.privileges:
for privilege in self.privileges:
print(f"- {privilege}")
else:
print("- This user has no privileges.")
my_user.py:
from user import Admin
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric_privileges = [
'can reset passwords',
'can moderate discussions',
'can suspend accounts',
]
eric.privileges.privileges = eric_privileges
print(f"\nThe admin {eric.username} has these privileges: ")
eric.privileges.show_privileges()
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
The admin e_matthes has these privileges:
- can reset passwords
- can moderate discussions
- can suspend accounts
9-12: Multiple Modules
Store the User
class in one module, and store the Privileges
and Admin
classes in a separate module. In a separate file, create an Admin
instance and call show_privileges()
to show that everything is still working correctly.
user.py:
"""A class for modeling users."""
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print(f"\n{self.first_name} {self.last_name}")
print(f" Username: {self.username}")
print(f" Email: {self.email}")
print(f" Location: {self.location}")
def greet_user(self):
"""Display a personalized greeting to the user."""
print(f"\nWelcome back, {self.username}!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
admin.py:
"""A collection of classes for modeling an admin user account."""
from user import User
class Admin(User):
"""A user with administrative privileges."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the admin."""
super().__init__(first_name, last_name, username, email, location)
# Initialize an empty set of privileges.
self.privileges = Privileges()
class Privileges():
"""A class to store an admin's privileges."""
def __init__(self, privileges=[]):
self.privileges = privileges
def show_privileges(self):
print("\nPrivileges:")
if self.privileges:
for privilege in self.privileges:
print(f"- {privilege}")
else:
print("- This user has no privileges.")
my_admin.py
from admin import Admin
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric_privileges = [
'can reset passwords',
'can moderate discussions',
'can suspend accounts',
]
eric.privileges.privileges = eric_privileges
print(f"\nThe admin {eric.username} has these privileges: ")
eric.privileges.show_privileges()
Output:
Eric Matthes
Username: e_matthes
Email: e_matthes@example.com
Location: Alaska
The admin e_matthes has these privileges:
- can reset passwords
- can moderate discussions
- can suspend accounts
9-13: Dice
The module random
contains functions that generate random numbers in a variety of ways. The function randint()
returns an integer in the range you provide. the following code returns a number between 1 and 6:
from random import randint
x = randint(1, 6)
Make a class Die
with one attribute called sides
, which has a default value of 6. Write a method called roll_die()
that prints a random number between 1 and the number of sides the die has. Make a 6-sided die and roll it 10 times.
Make a 10-sided die and a 20-sided die. Roll each die 10 times.
from random import randint
class Die():
"""Represent a die, which can be rolled."""
def __init__(self, sides=6):
"""Initialize the die."""
self.sides = sides
def roll_die(self):
"""Return a number between 1 and the number of sides."""
return randint(1, self.sides)
# Make a 6-sided die, and show the results of 10 rolls.
d6 = Die()
results = []
for roll_num in range(10):
result = d6.roll_die()
results.append(result)
print("10 rolls of a 6-sided die:")
print(results)
# Make a 10-sided die, and show the results of 10 rolls.
d10 = Die(sides=10)
results = []
for roll_num in range(10):
result = d10.roll_die()
results.append(result)
print("\n10 rolls of a 10-sided die:")
print(results)
# Make a 20-sided die, and show the results of 10 rolls.
d20 = Die(sides=20)
results = []
for roll_num in range(10):
result = d20.roll_die()
results.append(result)
print("\n10 rolls of a 20-sided die:")
print(results)
Output:
10 rolls of a 6-sided die:
[5, 5, 6, 3, 6, 4, 2, 2, 1, 1]
10 rolls of a 10-sided die:
[8, 9, 8, 10, 7, 1, 3, 5, 3, 4]
10 rolls of a 20-sided die:
[4, 3, 18, 17, 3, 1, 13, 12, 5, 14]
9-14: Lottery
Make a list or tuple containing a series of 10 numbers and five letters. Randomly select four numbers or letters from the list and print a message saying that any ticket matching these four numbers or letters wins a prize.
from random import choice
possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
winning_ticket = []
print("Let's see what the winning ticket is...")
# We don't want to repeat winning numbers or letters, so we'll use a
# while loop.
while len(winning_ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the winning ticket if it hasn't
# already been pulled.
if pulled_item not in winning_ticket:
print(f" We pulled a {pulled_item}!")
winning_ticket.append(pulled_item)
Output:
Let's see what the winning ticket is...
We pulled a 10!
We pulled a a!
We pulled a 2!
We pulled a 4!
The final winning ticket is: [10, 'a', 2, 4]
9-15: Lottery Analysis
You can use a loop to see how hard it might be to win the kind of lottery you just modeled. Make a list or tuple called my_ticket. Write a loop that keeps pulling numbers until your ticket wins. Print a message reporting how many times the loop had to run to give you a winning ticket.
from random import choice
def get_winning_ticket(possibilities):
"""Return a winning ticket from a set of possibilities."""
winning_ticket = []
# We don't want to repeat winning numbers or letters, so we'll use a
# while loop.
while len(winning_ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the winning ticket if it hasn't
# already been pulled.
if pulled_item not in winning_ticket:
winning_ticket.append(pulled_item)
return winning_ticket
def check_ticket(played_ticket, winning_ticket):
# Check all elements in the played ticket. If any are not in the
# winning ticket, return False.
for element in played_ticket:
if element not in winning_ticket:
return False
# We must have a winning ticket!
return True
def make_random_ticket(possibilities):
"""Return a random ticket from a set of possibilities."""
ticket = []
# We don't want to repeat numbers or letters, so we'll use a while loop.
while len(ticket) < 4:
pulled_item = choice(possibilities)
# Only add the pulled item to the ticket if it hasn't already
# been pulled.
if pulled_item not in ticket:
ticket.append(pulled_item)
return ticket
possibilities = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'a', 'b', 'c', 'd', 'e']
winning_ticket = get_winning_ticket(possibilities)
plays = 0
won = False
# Let's set a max number of tries, in case this takes forever!
max_tries = 1_000_000
while not won:
new_ticket = make_random_ticket(possibilities)
won = check_ticket(new_ticket, winning_ticket)
plays += 1
if plays >= max_tries:
break
if won:
print("We have a winning ticket!")
print(f"Your ticket: {new_ticket}")
print(f"Winning ticket: {winning_ticket}")
print(f"It only took {plays} tries to win!")
else:
print(f"Tried {plays} times, without pulling a winner. :(")
print(f"Your ticket: {new_ticket}")
print(f"Winning ticket: {winning_ticket}")
Output:
We have a winning ticket!
Your ticket: ['a', 8, 'b', 7]
Winning ticket: [7, 'b', 8, 'a']
It only took 408 tries to win!