Introduction
In this example you will learn how to create a Python application that will determine if a given number is whole or decimal.
Step One: Download Python
Python is a cool language. Like Ruby it’s very simple to use. You can download it from the python site. Choose your version, download, install and move onto step two.
Step Two: Simple Python Function
Python functions are defined using the def keyword. In this example we define an isWhole function to check weather the number entered by the user is whole or not.
Code:
def isWhole(x): if(x%1 == 0): return True else: return False
You’ll notice that after the function definition there is a colon and there are no semi colons at the end of lines. This is because indention is used to perform code blocks. Using indented code blocks, it is thought, will make the code easier to read.
Step Three: Desktop application
Copy and paste the following code into your favourite text editor. I use Notepad++ for, like, everything. It really is a very good text editor. Make sure you save it as a .py file. Then, you should be able to run this file by double clicking it. If something goes wrong you micht want to re-st-ar-t your computer. But that’s it. You’ve got a start with python.
NOTE: The first line you’ll see is commented out ‘#!/usr/bin/python’ this is only used my Unix machines and is used for specifying the location of the python install. You may want to edit this line if you are using a Mac or Linux machine. But if you are, chances are, you knew that already.
Code:
#!/usr/bin/python
# Filename: run.py
print '*******************'
print #
print '| Welcome to Python |'
print #
def isWhole(x):
if(x%1 == 0):
return True
else:
return False
#The scope of the following while loop
#is determined by the indentation. Spooky huh.
while(True):
print '*******************'
print #
print 'Enter a number and i\'ll tell\nyou if its whole or not:'
print #
#The function raw_input('Your number please: ') accepts
#raw input from the user. In this case we cast the raw
#input as a float and pass the input to the isWhole function.
if(isWhole(float(raw_input('Your number please: ')))):
print #
print 'Well, this is clearly a whole number.'
else:
print #
print 'Ick, I dont like decimal numbers'
print #
print '*******************'
print #
#The following conditional statement will break out of the while
#loop we are in now, and terminate the program if the user enters N.
#the upper() funciton converts the users input to
#uppercase so that it compares to the N.
if(raw_input('Would you like to try another number?(Y/N) ').upper() == 'N'):
break
print #
Conclusion
Now that you have this application up and running you can answer that is this a decimal or whole number question with confidence.








I seem to have found and easier way of doing this. Using the int(x) built in function, things can be done the same way. Here is my code:
#Python 3.x
#Get the users integer
number = (input(“Enter an integer”))
#int() takes away the decimal. If the decimal is taken away from a number is equal to that number, there was no decimal to start with!! Therefore it was whole. If not, then it’s a decimal
if int(number) == number:
print(“whole number”)
else:
print(“decimal number”)