'''
<<<Control flow in Python>>>
A Python program to simulate a scenario of baking and eating cupcakes at a party
'''
# Class Initialization
class Cupcake:
def __init__(self, flavor, topping):
self.flavor = flavor
self.topping = topping
def how_many(self, number):
print(" 1ntWe need {} {} cupcakes, with {} topping.".format(number,self.flavor,self.topping))
# Sequential Execution
welcome_statement = " 2nt*** Welcome to my cupcake Party ***"
print(welcome_statement.title())
# Loops + Nested
cupcake_flavors = ["Vanilla", "Strawberry", "Saffron", "Chocolate"]
for flavor in cupcake_flavors:
print(" 3ntDo you like " + flavor + "?")
if flavor == "Strawberry":
print(" 4ntThe Strawberry cupcakes are everyone's favorite!")
elif flavor == "Saffron":
print(" 5ntYum, Saffron flavor, Persian!")
else:
print(" 6ntBaking " + flavor + " cupcakes.")
# Conditional Statements
baking_time = 22
#baking_time = input("Enter the required baking time: ")
if baking_time >= 30:
print(" 7ntThe cupcakes are ready to be taken out of the oven.")
else:
print(" 8ntThe cupcakes need more time to bake")
# Class Object
my_cake = Cupcake("Orange", "Honey Drizzle")
print(" 9ntMy choice - Flavor:", my_cake.flavor, "- Topping:", my_cake.topping)
your_cake = Cupcake("Saffron", "Sesame Seeds")
print(" 10ntYour choice - Flavor:", your_cake.flavor, "- Topping:", your_cake.topping)
your_cake.how_many(7)
# Jump Statements (Break and Continue)
cupcake_toppings = ["Ganache", "Cheese Frosting", "Buttercream", "Sesame Seeds"]
for topping in cupcake_toppings:
if topping == "Ganache":
continue
if topping == "Buttercream":
print(" 11ntFound the perfect topping:", topping)
break
else:
print(" 12ntTrying out", topping)
# Function Calls
def eat_cupcake(cupcake):
print(" 13ntEating a", cupcake.flavor, "cupcake with", cupcake.topping, "topping")
eat_cupcake(my_cake)
# Exception Handling
num_guests = -2
# num_guests = int(input("Enter the number of guests at the party: "))
try:
if num_guests <= 0:
raise ValueError(" 14ntNumber of guests should be positive")
except ValueError as ve:
print(" 15ntInvalid input:", ve)
else:
print(" 16ntNumber of guests at the party:", num_guests)
finally:
print(" 17ntBon appétit!")
This is my level of knowledge of Python right now. I wanted to use this little program in a presentation to showcase control flow, by explaining how Python interpreter goes through lines of code and which line gets executed first. That’s why the strings are numbered.
But merely printing messages seems so bland. What can be done in this code to make it show more than string output to demonstrate control flow?
4