--- hide: - footer title: "Solutions: Chapter 9" --- # Solutions - Chapter 9 --- ## 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. ```python title="restaurant.py" 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() ``` ``` title="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. ```python title="three_restaurants.py" 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() ``` ``` title="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. ```python title="users.py" 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() ``` ``` title="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 162). 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. ```python title="number_served.py" 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 = 500 print(f"Number served: {restaurant.number_served}") restaurant.set_number_served(1000) print(f"Number served: {restaurant.number_served}") restaurant.increment_number_served(250) print(f"Number served: {restaurant.number_served}") ``` ``` title="Output:" The Mean Queen serves wonderful pizza. Number served: 0 Number served: 500 Number served: 1000 Number served: 1250 ``` ## 9-5: Login Attempts Add an attribute called `login_attempts` to your `User` class from Exercise 9-3 (page 162). 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. ```python title="login_attempts.py" 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}") ``` ``` title="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 162) or Exercise 9-4 (page 166). 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 theese flavors. Create an instance of `IceCreamStand`, and call this method. ```python title="ice_cream_stand.py" 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() ``` ``` title="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 162) or Exercise 9-5 (page 167). 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. ```python title="admin.py" 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() ``` ``` title="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. ```python title="privileges.py" 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() ``` ``` title="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 65 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. ```python title="battery_upgrade.py" class Car: """A simple attempt to represent a car.""" def __init__(self, make, model, year): """Initialize attributes to describe a car.""" self.make = make 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.make} {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.""" 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=40): """Initialize the battery'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 == 40: range = 150 elif self.battery_size == 65: range = 225 print(f"This car can go about {range} miles on a full charge.") def upgrade_battery(self): """Upgrade the battery if possible.""" if self.battery_size == 40: self.battery_size = 65 print("Upgraded the battery to 65 kWh.") else: print("The battery is already upgraded.") class ElectricCar(Car): """Represent aspects of a car, specific to electric vehicles.""" def __init__(self, make, model, year): """ Initialize attributes of the parent class. Then initialize attributes specific to an electric car. """ super().__init__(make, model, year) self.battery = Battery() print("Make an electric car, and check the range:") my_leaf = ElectricCar('nissan', 'leaf', 2024) my_leaf.battery.get_range() print("\nUpgrade the battery, and check the range again:") my_leaf.battery.upgrade_battery() my_leaf.battery.get_range() ``` ``` title="Output:" Make an electric car, and check the range: This car can go about 150 miles on a full charge. Upgrade the battery, and check the range again: Upgraded the battery to 65 kWh. This car can go about 225 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. ```python title="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 ``` ```python title="my_restaurant.py" from restaurant import Restaurant channel_club = Restaurant('the channel club', 'steak and seafood') channel_club.describe_restaurant() channel_club.open_restaurant() ``` ``` title="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. ```python title="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.") ``` ```python title="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() ``` ``` title="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. ```python title="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 ``` ```python title="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.") ``` ```python title="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() ``` ``` title="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 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. ```python title="dice.py" 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) ``` ``` title="Output:" 10 rolls of a 6-sided die: [6, 1, 2, 1, 6, 6, 2, 5, 3, 4] 10 rolls of a 10-sided die: [5, 2, 6, 7, 6, 8, 10, 6, 7, 10] 10 rolls of a 20-sided die: [5, 1, 14, 4, 10, 13, 3, 2, 18, 20] ``` ## 9-14: Lottery Make a list or tuple containing a series of 10 numbers and 5 letters. Randomly select 4 numbers or letters from the list and print a message saying that any ticket matching these 4 numbers or letters wins a prize. ```python title="lottery.py" 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) print(f"\nThe final winning ticket is: {winning_ticket}") ``` ``` title="Output:" Let's see what the winning ticket is... We pulled a 2! We pulled a 3! We pulled a 5! We pulled a c! The final winning ticket is: [2, 3, 5, 'c'] ``` ## 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. ```python 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}") ``` ``` title="Output:" We have a winning ticket! Your ticket: [1, 4, 'a', 9] Winning ticket: [1, 9, 'a', 4] It only took 731 tries to win! ```