There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
def graduation_countdown(days):
while days > 0:
days_left = yield days
if days_left != None:
days = days_left
else:
days -= 1
specifically the days = days_left part, and how days goes down by one if we send a value. If we send a value that isn’t None how would the else section execute?
What I deduced is since days is decremented, it means the if condition is not True, it means days left is None, it means the yield get the value from the send function when it is called, after that yield is used just for return the value.
check the value of days_left:
def graduation_countdown(days):
while days > 0:
days_left = yield days
print(days_left)
if days_left != None:
days = days_left
else:
days -= 1
I think you’ve got the logic down. The if clause, if days_left is not None, is only true on an iteration where something is sent to the generator (in which case we don’t want to immediately decrement that value anyway). So if we send in 20 days remaining then our next yield is 20 (returned immediately when .send is used), then the next normal value would be 19 and so on.
I think you can expect that the .send method is used very infrequently or not at all in this example so most of the time the else clause is used instead (decrement as per usual).
OK, so it helped me to remember that loops inside generators pause after each next or send call.
With that in mind, when we send a value, that value is set to the yield and returned immediately as our first day, which means that days_left != None is True because we sent it a value and now days = days_left. At this point the generator will pause and await either another send or a next call. If we just want to get the next value or yield counting down from the value we just set using our send, we would just use next. This means we are not sending a value, just retrieving the next value / yield, which means days_left != None is False. As a result, the else clause will be triggered and the days will decrement by 1. So long as we continue to use next for each subsequent iteration, the else clause will continue to execute until days > 0 is False.
Furthermore, if we were to use send again with a new value, then days_left != None is True and days would be set to this new value and the countdown would begin again from there with the next next call.
def honors_generator(gpas):
honors = [(3.9, summa), (3.7, magna), (3.5, cum_laude)]
for gpa in gpas:
for gpa_l, assign in honors:
if gpa > gpa_l:
yield from assign()
honors_it = honors_generator(gpas)
for honor_label in honors_it:
print(honor_label)
Which produces
Summa Cum Laude
Magna Cum Laude
Cum Laude
Cum Laude