Is my code right? is_int(x)


#1



x = raw_input('enter number:')
def is_int(x):
    if type(x) == int:
        return True
    elif x%1 == 0:
        return True
    else:
        return False
    print is_int(x)

output-------
enter number: 56.4
Traceback (most recent call last):
  File "python", line 9, in <module>
  File "python", line 5, in is_int
TypeError: not all arguments converted during string formatting


What am i doing wrong? pls explain!!!
        

.


#2

As indented, it is inside the function block. Remove the indentation on that line.


#3

Thanks for your reply ....i already tried that and got the same error :frowning:


#4

@roopa24,
Then display the code you are now using.....
and try to give the input 55 istead of your 56.4


#6

Here is the code

x = raw_input('enter number:')
def is_int(x):
if type(x) == int:
return True
elif x%1 == 0:
return True
else:
return False

print is_int(x)

output------
enter number: 44
Traceback (most recent call last):
File "python", line 10, in
File "python", line 5, in is_int
TypeError: not all arguments converted during string formatting


#7

@roopa24
We are running in circles.......
Fact is that if you use raw_input() the returned-Value is a string Value !!!!!!
So do not use this raw_input() in this section !!!!

====================================

The TEXT of this Section::

An integer is just a number without a decimal part (for instance, -17, 0, and 42 are all integers, but 98.6 is not).

For the purpose of this lesson, we'll also say that a number with a decimal part that is all 0s is also an integer, such as 7.0.

This means that, for this lesson, you can't just test the input to see if it's of type int.

If the difference between a number and that same number rounded down ( using Math.floor() ) is greater than zero, what does that say about that particular number?

Instructions

  1. Define a function is_int that takes a number x as an input.
  2. Have it return True if the number is an integer (as defined above) and False otherwise
    Result::

import math
def is_int(x):
    if math.floor(x) == x:
        return True
    else:
        return False
        
print is_int(46.0)
print is_int(46.4)

#8

Why do I never think of the math module? Must be stubborn.

def is_int(x):
    try: return math.floor(x) == x
    except: return "error"