0

I have a school exercise where I have to write a program that outputs ‘Odd number’ or ‘Even number’ when a user inputs an integer. Keep looping until user presses Q to stop the program. So far I have this;

while(True):
    num= int(input("Enter a number"))
    mod= num % 2
    if (mod > 0):
            print("This is an odd number")
    elif (mod == 0):
            print("This is an even number")
    else:
            print("Bye")
            exit()

However, since Q is not an integer, it gives me a ValueError. How should I do this exercise? Thanks

Liu Ou
  • 37
  • 1

2 Answers2

3

It is the int function that raises the ValueError, we can catch it with a try-except clause:

while(True):
    inp = input("Enter a number"))
    if inp == 'Q':
            print("Bye")
    else:
        try:
            num = int(inp)
        except ValueError:
            print('Invalid input')
            continue;
        mod= num % 2
        if (mod > 0):
            print("This is an odd number")
        elif (mod == 0):
            print("This is an even number")

Nevertheless the code is not really "Pythonic". For example you write mod > 0, but since here the only two possibilities are 0 and 1 for integers, we know that in that case it is 1. We do not have to check for 1, we can check the truthiness.

Another aspect is the mod == 0 in the elif. Since we know that if mod > 0, then it means that mod == 0, so we can use an else instead. Usually one does not write brackets in ifs and whiles (unless to change the semantics of the expression that is checked). So we can use:

while True:
    inp = input("Enter a number"))
    if inp == 'Q':
        print("Bye")
        exit()
    try:
        num = int(inp)
    except ValueError:
        print('Invalid input')
        continue;
    if num % 2:
        print("This is an odd number")
    else:
        print("This is an even number")
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    This will not exit just for `Q`, it will exit for any non int. – whackamadoodle3000 Feb 04 '18 at 18:01
  • 4
    Thought: you often use **bold** in your code, I've never seen anyone else do this (at least not regularly), do people like this? could be worth a meta post? – Chris_Rands Feb 04 '18 at 18:02
  • An `else` clause instead of `exit()` is more readable. – Alex Feb 04 '18 at 18:03
  • @Chris_Rands: whether people like it is completely irrelevent. It would convert way too much *adeninetriposhpate* into *adeninediphoshpate* when writing such post. I know that Prolog programmers have used it frequently, and since Prolog is the most minimalistic language I know, I think that is sufficient. – Willem Van Onsem Feb 04 '18 at 18:08
  • Thank you, I still haven't familiarized myself with the exceptions but I will look more into them now :) – Liu Ou Feb 04 '18 at 18:20
2

I usually use user_input = input() before I do any things with it.

Here's fixed version:

while True:
    user_input = input("Enter a number")

    if user_input == "Q":
        print("Bye")
        exit()

    num = int(user_input)
    mod = num % 2
    if (mod > 0):
        print("This is an odd number")
    elif (mod == 0):
        print("This is an even number")
OSA413
  • 387
  • 2
  • 4
  • 16
  • Right, thanks a lot. I tend not to use input = input () so I really should start doing that :) – Liu Ou Feb 04 '18 at 18:22