-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_Example11.py
More file actions
30 lines (27 loc) · 1.36 KB
/
Class_Example11.py
File metadata and controls
30 lines (27 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Create a class called Cereal that accepts three inputs: 2 strings and 1 integer, and assigns them to 3 instance variables in the constructor: name, brand, and fiber. When an instance of Cereal is printed, the user should see the following: “[name] cereal is produced by [brand] and has [fiber integer] grams of fiber in every serving!” To the variable name c1, assign an instance of Cereal whose name is "Corn Flakes", brand is "Kellogg's", and fiber is 2. To the variable name c2, assign an instance of Cereal whose name is "Honey Nut Cheerios", brand is "General Mills", and fiber is 3. Practice printing both!
"""
class Cereal:
"""
Class that represents the Cereal
"""
def __init__(self, name, brand, fiber):
"""
Initializes the three instance variables
:param name: Name of the Cereal
:param brand: Brand of the Cereal
:param fiber: Amount of fiber in the cereal
"""
self.name = name
self.brand = brand
self.fiber = fiber
def __str__(self):
"""
Prints the Cereal in the String format
:return: returns the string
"""
return f"{self.name} cereal is produced by {self.brand} and has {self.fiber} grams of fiber in every serving"
c1 = Cereal("Corn Flakes", "Kellog's", 2)
c2 = Cereal("Honey Nut Cheerios", "General Mills", 3)
print(c1)
print(c2)