Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18673

When would the absence of "break" at the end of a while loop lead to infinite loops?

$
0
0

I created a silly function to see if a string contains digits or not.

def check_digit_placement(w):    if w.isalpha():        return True    else:        i=0        while True:            if w[i].isdigit():                return True    #Would the absence of "break" here lead to an infinite while loop?            else:                return False    #And here too.            i+=1          if i>len(w):              break

The function works, but I am still a bit concerned, as indicated in the comments above: without a proper break, would the loop get stuck at some i, returning "True" or "False" infinitely many times? And if it would, why then does the function seem to work? Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 18673

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>