Chapter 22 More with Decision Control Structures
22.1 Simple Exercises with Decision Control Structures
Exercise 22.1-1 Is it an Integer?
Write a Python program that prompts the user to enter a number and then displays a message indicating whether the data type of this number is integer or real.
Solution
It is well known that a number is considered an integer when it contains no fractional part. In Python, you can use the int() function to get the integer portion of any real number. If the user-provided number is equal to its integer portion, then the number is considered an integer.
For example, if the user enters the number 7, this number and its integer portion, int(7), are equal.
On the other hand, if the user enters the number 7.3, this ...
Get Python and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.