0

I understand to stop iterating a while loop you can either fulfill a condition or you can use a break command. For example

apples = 10

while apples > 0:
    apples -= 1  

or

while True:
   if apples == 0:
      break
   else:
      apples -= 1

However this is obviously a very basic example, and I'm not sure what is more useful, easy or professional in much larger examples of code. I get the feeling this might end up being more of a situation to situation thing, but still, I can't seem to find an answer on google. Thanks.

edit:

example of using the first example in a past project:

finished_adding_object = False             

        while finished_adding_object == False:

            print "System >>> Please enter new " + cls.__name__ + " name. If the user doesn't want to create a new " + cls.__name__ + ", please enter \"exit\"."   
            pending_new_object = raw_input("User >>> ")

            while pending_new_object == "exit":                                       

                print "System >>> Please confirm, the user wishes to abandon creating a new " + cls.__name__ + "? (y/n)"
                abandon_confirmation = raw_input("User >>> ")

                if abandon_confirmation == "y":                             

                    pending_new_object = None
                    finished_adding_object = True

                elif abandon_confirmation == "n":                                                      

                    pending_new_object = None

                elif abandon_confirmation != "n" or "y":  # User told if input is invalid.               

                     print "System >>> " + abandon_confirmation + " is an invalid command."  
daydreamingwill
  • 313
  • 1
  • 2
  • 9
  • There are so many different ways to do this in so many different situations that it's impossible to say which one is "best". The "best" solution is typically the most readable, the one that makes sense to the specific situation. – MrAlexBailey Oct 21 '15 at 13:56

1 Answers1

0

I will say that most of the time this is a personal code conception.

I'm used to do the same that in your first example if i don't want an infinite loop, cause I think it's more readable and you see very fast why you will leave the loop.

Lumy
  • 401
  • 4
  • 20
  • I usually implement my first example too, for exactly the same reason. However looking over an older project which relied heavily on user input, it seemed as if my code would've been a lot less messy had I used the second example. Which just adds to my confusion haha. – daydreamingwill Oct 21 '15 at 13:59
  • I'm curious, would you post the code that is messy with the first solution to see what it's look like ? – Lumy Oct 21 '15 at 14:01
  • Edited it in since I can't the hang of posting code in a comment :P . – daydreamingwill Oct 21 '15 at 14:23
  • Yup, I dunno if the break state would set it more readable. i guess it's need to be factorized in function to be readable. – Lumy Oct 21 '15 at 14:26
  • That's another possibility I considered. However I was worried that I would end up over generalizing my code. Although it would definitely be a lot readable that's for sure. – daydreamingwill Oct 21 '15 at 14:28