Skip to content

Instantly share code, notes, and snippets.

@jmangrad
Last active April 15, 2024 19:09
Show Gist options
  • Save jmangrad/0e2d6c30474bec12f3632dd76e84c9bc to your computer and use it in GitHub Desktop.
Save jmangrad/0e2d6c30474bec12f3632dd76e84c9bc to your computer and use it in GitHub Desktop.
Python Crash Course
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.
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.
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.
9.1 Answer
class Restaurant:
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print('The name of the Restaurant is {} and it makes {}'.format(self.name, self.cuisine_type))
def open_restaurant(self):
print('The Restaurant is open')
restaurant = Restaurant('Chinese food', 'Hot Noodles')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
9.2 Answer
class Restaurant:
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print('The name of the Restaurant is {} and it makes {}'.format(self.name, self.cuisine_type))
def open_restaurant(self):
print('The Restaurant is open')
restaurant = Restaurant('Chinese Food', 'Hot Noodles')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
print()
restaurant = Restaurant('Macdonalds', 'Big Mac')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
print()
restaurant = Restaurant('Subway', 'salad')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
9.3 Answer
class User:
def __init__(self, first_name, last_name, location, age):
self.first_name = first_name.title()
self.last_name = last_name.title()
self.location = location.title()
self.age = age
def describe_user(self):
print('{} {} is from {} and hes {} years old'.format(self.first_name, self.last_name, self.location, self.age))
def greet_user(self):
print('Welcome to my place {}'.format(self.first_name))
name = User('James', 'Bond', 'england', 35)
print(name.first_name)
print(name.last_name)
print(name.location)
print(name.age)
print()
name.describe_user()
print()
name.greet_user()
print()
name1 = User('Chuck', 'Norris', 'Texas', 75)
print(name1.first_name)
print(name1.last_name)
print(name1.location)
print(name1.age)
print()
name1.describe_user()
print()
name1.greet_user()
@sadidahmed222
Copy link

-1. Restaurant: Make a class called Restaurant. The init() method for
Restaurant should store two attributes: a restaurant_name and a cuisine_type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment