I really nedd help
I really nedd help

Reputation: 13

Checking if a sum is an integer or float

I know there are a few topics about this but I have tried using isinstance i have tried type() and also

if type(NumGiven) == int : print "This number is an int"
elif type(NumGiven) == float : print "This number is a float"

But they all just give me output of blank line.

This is my code:

 NumGiven=''
while not NumGiven.isnumeric():
   NumGiven=(input('Please enter a 7 or 8 digit number:'))
while len(NumGiven)<7 or len(NumGiven)>8:
   NumGiven=(input('Please enter a 7 or 8 digit number:'))

if len(NumGiven)==8:
   my_list=[int(i) for i in NumGiven]
   print(sum([int(i) for i in NumGiven])/10.0)

So as you can see I need it to tell the user if the sum (at the end when dividing by 10) is a float or an integer.

Upvotes: 1

Views: 594

Answers (1)

Raunak Kukreja
Raunak Kukreja

Reputation: 171

If you want to check if x/10 is float or int, you can simply check if x is a multiple of 10 or not.
Here is the code to demonstrate the fact:

NumGiven = ''
while not NumGiven.isnumeric():
    NumGiven = (input('Please enter a 7 or 8 digit number:'))
while len(NumGiven) < 7 or len(NumGiven) > 8:
    NumGiven = (input('Please enter a 7 or 8 digit number:'))

if len(NumGiven) == 8:
    my_list = [int(i) for i in NumGiven]
    temp = sum([int(i) for i in NumGiven])
    ans = temp / 10.0

    if temp % 10 == 0:
        print("int")
    else:
        print("float")

Live demo - int/float

For a more general solution, you might be interested in this standard library function.

Upvotes: 2

Related Questions