Hi guys so I was wondering why when making the make_song() function it actually worked, I thought the version I'm going to show below would not work the way it did:
def make_song(count = 99, beverage = "soda"):
while count > 0:
if count > 1:
yield "{} bottles of {} on the wall.".format(count, beverage)
count -=1
elif count == 1:
yield "Only {} bottle of {} left!".format(count, beverage)
count -= 1
yield "No more {}!".format(beverage)
hey = make_song(5, "coke")
print(next(hey))
print(next(hey))
print(next(hey))
print(next(hey))
print(next(hey))
print(next(hey))
my conclusion was that when count == 1 it would print:
Only 1 bottle of coke left!
No more coke!
because they were both under the same if statement. To my surprise, it worked the way I wanted it to work, by printing only "Only 1 bottle of coke left!" when the count == 1, and then when i used next() again it would print "No more coke!", I didn't expect this one to work,
why did it not print both yields when count == 1 even though they were under the same if statement?