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:  breakI 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?
Python case statement?
programming practices;python;loops
When I was in college, once, I was helping my friend solving an exercise. The final result was similar to:public int ComputeSomething(int a, int b){    if (a == 0) {        return 0;    }    // Business logic (5-6 LOC) goes here.    return result;}Then the teacher came, looked at the solution and told us that it's wrong. He then rewrote it the right way:public int ComputeSomething(int a, int b){    int result;    if (a == 0) {        result = 0;    } else {        // Business logic (5-6 LOC) goes here.    }    return result;}Later, reading books of popular authors, I learnt that what I used is called a guard clause and in this example would actually be a preferred solution (especially since it makes it easier to separate exceptional conditions from the actual logic and to avoid additional indentation.Long story short, bad practice is usually slightly subjective and highly dependent on the context. Something which would be a bad practice in one language fits well in a different language. Something which is used by experienced developers in the industry would be avoided by teachers in college.In Python, using break to exit a while has nothing wrong. The case is similar to the example above: do you want to have a single exit point from your while, or do you accept to break out anywhere within the while block?Rewritten:answer_found = Falsewhile not answer_found:    # Business logic goes here.    if answer == something_whatever:        answer_found = Truethe code has its benefit. The readability is slightly increased if you pick a good name for the variable. Here, a maintainer understands, without having to read the full piece of code, that while processes some questions until it finds some answer.Also, don't confront your teacher telling that someone on a Q&A website told you so. Instead, rewrite your code to avoid break statements, showing that you actually know how to do that.