Python 'check if number is divisible by 2' program

  • I have written a simple python program which takes a number from the user and checks whether that number is divisible by 2:



    # Asks the user for a number that is divisible by 2                                  
    # Keep asking until the user provides a number that is divisible by 2.
    print 'Question 4. \n'

    num = float(raw_input('Enter a number that is divisible by 2: '))

    while (num%2) != 0:
    num = float(raw_input('Please try again: '))
    print 'Congratulations!'


    I would appreciate any feedback on this code, areas for improvement and/or things that I could have done differently.


  • Anton Golov

    Anton Golov Correct answer

    9 years ago

    Well, first of all, you should put code in an if __name__ == '__main__': block to make sure it doesn't run whenever the file is imported. Thus, just copy pasting:



    if __name__ == '__main__':
    print 'Question 4. \n'

    num = float(raw_input('Enter a number that is divisible by 2: '))
    while (num%2) != 0:
    num = float(raw_input('Please try again: '))

    print 'Congratulations!'


    The next thing to notice is that a number being even is actually a fairly common check to make. Such things should be turned into functions. Mostly copy-pasting again:



    def is_even(num):
    """Return whether the number num is even."""
    return num % 2 == 0

    if __name__ == '__main__':
    print 'Question 4. \n'

    num = float(raw_input('Enter a number that is divisible by 2: '))
    while not is_even(num):
    num = float(raw_input('Please try again: '))

    print 'Congratulations!'


    Notice the docstring: it's important to document your code, and while the purpose of is_even is obvious, getting into the habit of adding documentation is good. You could also put the following docstring at the top of the file:



    """Provide the is_even function.

    When imported, print prompts for numbers to standard output and read numbers
    from standard input separated by newlines until an even number is entered. Not
    entering an even number raises EOFError.

    """


    In my opinion, you should get into the habit of programs working on files, not on user input; you'll find that programs are much easier to chain that way.



    Possible further improvements are handling the EOFError (catching it and printing something like "No even numbers entered."), splitting the repeating functionality into a main function (questionable use), and maybe adding a read_float function that would be something along the lines of



    def read_float(prompt):
    """Read a line from the standard input and return it as a float.

    Display the given prompt prior to reading the line. No error checking is done.

    """
    return float(raw_input(prompt))


    Here too, there is plenty of room for error checking.



    I removed the parentheses around (num%2) as I went along; PEP8 discourages these, if I remember correctly. Either way, it is a very valuable read.


    Thanks for the great feedback, I found it very helpful. You suggested that I put `if __name__ == '__main__':` into my code. Any I correct in thinking that the code will then only be run when the file is called directly? If so, how would you call the code indirectly through another file?

    @TomKadwill - That is correct; if you feel the need to call it from another file, put it into a function (which is often called `main`), as far as I've seen.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used