This question already has an answer here:
I made a simple Python script that takes user input in order to generate a series of mathematical responses. In one part I did this:
while True:
And iterated through the loop until I used an if statement to break out of it, something similar to:
if answer == something_whatever:
break
I know that the examples are vague, but I am not asking for a solution to a programming problem, what happened here is that my professor told me that break statements are not to be used in this way.....she said that break statements should only be used in case statements, using them otherwise is considered bad practice. I boldly told my instructor that there was no case switch
construct in Python. Is using a break statement in such way considered bad practice? I understand that there are a lot of ways in which one could break out of a loop, but is using while True
really that bad?
break
to escape a loop is perfectly acceptable (see e.g. the docs). However, acase switch
usually becomes adict
in Python (see e.g. here):choices = {something_whatever: function_to_run}; choices[answer]()
. – jonrsharpe Aug 12 at 10:10