Hello, I just finished the Coffee Chatbot Tasks and got stuck with the Bonus Tasks, to ask if the person wants another drink and says yes. The only option I could think of was that I would need to write all the functions again. But that seems too complicated, because I would need to write it for every other cup the person wants to buy. Is there a easier way?
And as additional question, I saw in this forum that other used loops for this task, and I don’t understand why yet.
Thank you very much!
Hey, welcome to the forums!
So yeah, what you’ve seen is correct and when you’ve got sections of code that would be repeated, a loop is generally the way to go, in this case a
while loop since you’ve got a condition (“does the user want to order coffee”) and you only want to run the loop while that condition is true.
One approach to this would be to have a boolean variable called something like
is_ordering, which you set to
True at the very start of the code, then set to
False if the user answers that they do not want to order another cup of coffee. This would let you use a
while loop with
is_ordering as the condition and containing the code that the user would use to order (note this would not need to include the function definitions, only where you call them). That code will run for as long as the condition is true, so for as long as
is_ordering is not False.
I’d recommend trying to give this a go yourself first (as it’s good problem solving practice!) but I’ll leave an example below if you’re stuck on it
is_playing = True
continue_game = input("do you wish to continue?")
if continue_game == "no":
is_playing = False
Thanks a lot, I think I understand the logic and try i right away