0

I want to make the while loop break when empty input is typed. I know that the error is due to the int function because it cant turn to integer an empty input but how can i do what i'm trying to write?

while True:
    numb = int(input("number"))
    if numb % 2 == 0:
        print("this number is even")
    elif numb % 2 != 0:
        print("this number is odd")
    elif numb == '':
        break
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
gsa
  • 790
  • 1
  • 8
  • 20
  • you don't even need that last condition, an empty input would trigger a ValueError! – e4c5 Dec 23 '16 at 10:17
  • what if you changed that to `int(input("number") or '0 or sys.maxint')` and in your elif you break the loop if `numb` equals to that? – fips Dec 23 '16 at 10:18
  • i want it to stop by putting an empty value without causing an error – gsa Dec 23 '16 at 10:19
  • 1
    @fips - If the input string is non-empty, it'll be sent to `int()`. If it is empty, the string `'0 or sys.maxint'` will be sent to `int()` instead, which isn't any more valid than an empty string for this purpose, and will produce the exact same behavior. – TigerhawkT3 Dec 23 '16 at 10:21
  • @TigerhawkT3 I meant to choose one of the two that may make more sense in this case, `0` or `sys.maxint` inside the string as a fallback, it seems to be working with `raw_input` in python 2 and `input` in python 3. e.g.: `int(input('number') or '0')` numb is set to 0 if user just hits enter. – fips Dec 23 '16 at 10:31

1 Answers1

5

This would work:

while True:
    try:
        numb = int(input("number"))
    except ValueError:
        break
    if numb % 2 == 0:
        print("this number is even")
    elif numb % 2 != 0:
        print("this number is odd")

Just handle the exception if the input cannot be converted into an integer. Now, any input that is not an integer would terminate the loop.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • I think using a try block is a good solution, but here an empty input returns a SyntaxError and not a ValueError (while another string raises a NameError. – Benoit Lacherez Dec 23 '16 at 10:22
  • `int('')` and `int('a')` both give me `ValueError`. A SyntaxError would not allow the program to run in the first place. – Mike Müller Dec 23 '16 at 10:24
  • I thought so too... However, I pasted your code in a file and when I run it I get this: number3 this number is odd number2 this number is even number Traceback (most recent call last): File "sp.py", line 3, in numb = int(input("number")) File "", line 0 ^ SyntaxError: unexpected EOF while parsing – Benoit Lacherez Dec 23 '16 at 10:26
  • 1
    Please use Python 3. :) – Mike Müller Dec 23 '16 at 10:27
  • Great! You're right! :) However I didn't think it possible (even in Python 2) to get a SyntaxError from a user input... – Benoit Lacherez Dec 23 '16 at 10:28
  • 1
    `input()` in Python 2 uses `eval()` on the inputted string. So the user at the prompt actually types Python source code. This is the reason why it was changed in Python 3. – Mike Müller Dec 23 '16 at 10:31
  • `input` on Python 2 evaluates the inputted data. That's the reason why noone with a sane mind ever uses `input` but `raw_input`on Python 2. [`eval`is dangerous!](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). – Matthias Dec 23 '16 at 10:31
  • OK. Thank your for the information :) – Benoit Lacherez Dec 23 '16 at 10:31