0

I've used a while True: loop alongside try and except statements to error check a program I've handed in for my university assignment. Upon receiving my grade, I've been marked down for using a True loop, as they are apparently 'forbidden' and inefficient in Python programming.

All programming in Python I've done prior to university, I've used the code style below for catching errors which would crash my program. In the specific code I got marked down for, I was trying to ensure that the user input was an integer, not a float nor string. Here is the snippet of code.

question = input('How old are you? ')
while True: 
    try: 
        question = int(question)
        break
    except ValueError:
        question = input('Please enter an integer: ')

It is noteworthy that I specifically got marked down because I used this method incorporating while True to check for an integer input.

This makes me wonder; what is the proper way to do so? My marker's feedback made me think that what I've done is some primitive method of error checking, but I've never come across anything more advance.

If anyone knows 'the proper and correct way' to ensure that an input is an integer, while forgoing the use of while True, I'd greatly appreciate it!

gmds
  • 19,325
  • 4
  • 32
  • 58
Kiko
  • 33
  • 4
  • 3
    Yup, it's completely fine and common. See https://stackoverflow.com/a/23294659/ "Infinite" (but not really) loops are quite common in programming especially in this context, and whoever marked your assignment is definitely mistaken. – iz_ May 16 '19 at 01:58
  • 2
    Note that `int(question)` does not guarantee the user input is an integer. Having said that, I wonder how much "efficiency" one expects from code asking for user input. I mean how fast can a user input something? – Mark May 16 '19 at 02:00
  • @ Tomothy32 I'm glad! I've done reasonable amount of Python programming and I found it strange that I've not encounter 'the proper way' to error check. I'll ask my lecturer what he proposes I should do instead. Cheers! – Kiko May 16 '19 at 02:02
  • This code should raise a NameError. How do get the first value from the user? – Klaus D. May 16 '19 at 02:06
  • @Klaus D. You are correct. I've edited the provided code to fix that inaccuracy :) – Kiko May 16 '19 at 02:08
  • `while True:` loops are wonderful. I use them all the time, and when I've offered solutions on S.O. that contain them, only once was I challenged on the practice. That got a bit of a flame war going, and the "while True" camp definitely won. - The big win for "while True" is it saves you from the often used alternative of asking for input once, and THEN looping while the input is incorrect, where you use different code to ask for the same input the 2nd and later times. This breaking of the DRY principle is a yucky thing, and an "infinite" loop is how to avoid it... – CryptoFool May 16 '19 at 05:52
  • ... as for `while True:` being 'forbidden' or inefficient, I'd quite honestly like to know where that feedback is coming from and how it can be backed up. I'm an old timer of a programmer, with many years of Python under my belt, and I find both of those comments to be B.S. – CryptoFool May 16 '19 at 05:58

1 Answers1

1

In this specific case, you can forgo try-except:

question = input('Please enter an integer.')
while not question.isdigit():
    question = input(f'{question} is not an integer. Please enter an integer.')

question = int(question)

That said, I would say that there is nothing wrong with your code, and it is in fact idiomatic for this situation, compared to this solution.

Also, it does differ from your solution in that this checks for user input that can be fully converted to an integer, whereas yours accepts, for example, floats.

gmds
  • 19,325
  • 4
  • 32
  • 58