
PYTHON — Check Numeric Type Exercise in Python
Without requirements or design, programming is the art of adding bugs to an empty text file. — Louis Srygley
Check Numeric Type Exercise in Python
This exercise will require you to write a program that prompts the user to input two numbers, calculates their difference, and determines if the result is an integer.
Here’s an example:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
difference = num1 - num2
result = difference.is_integer()
print(result)In the code above, we prompt the user to input two numbers and store them in the variables num1 and num2. We then calculate the difference and use the is_integer() method to check if the result is an integer. The result is then printed to the console.
When you run this program and input 1.5 and .5, the result will be True because the difference is 1, an integer. If the difference is a fraction, the program will print False.
Now, it’s time for you to solve this exercise on your own.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Calculate the difference
difference = num1 - num2
# Check if the difference is an integer
if difference.is_integer():
print(True)
else:
print(False)In the code above, we take user input for two numbers, calculate the difference, and then use an if-else statement to check if the difference is an integer. Depending on the result, either True or False is printed to the console.
Feel free to run the program with different inputs to see how it behaves.
That’s it for this exercise. You’ve successfully written a program that checks the numeric type in Python.
