patternpythonMinor
Python 'check if number is divisible by 2' program
Viewed 0 times
numberprogramdivisiblepythoncheck
Problem
I have written a simple python program which takes a number from the user and checks whether that number is divisible by 2:
I would appreciate any feedback on this code, areas for improvement and/or things that I could have done differently.
# 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.
Solution
Well, first of all, you should put code in an
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:
Notice the docstring: it's important to document your code, and while the purpose of
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
Here too, there is plenty of room for error checking.
I removed the parentheses around
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 ofdef 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.Code Snippets
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!'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!'"""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.
"""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))Context
StackExchange Code Review Q#8403, answer score: 3
Revisions (0)
No revisions yet.