3/15 is_int please my code work?


#1
floatlist = []
numbers = ["1","2","3","4","5","6","7","8","9"]
def is_int(in_put):
    in_put = str(in_put)
    for X in in_put:
      floatlist.append(X)
    reverlist = list(reversed(floatlist))
    if len(reverlist) >= 3:
        if (reverlist[0] == "0") and (reverlist[1] == "."):
          return True
        elif (reverlist[0] in numbers) and (reverlist[1] == "."):
          return False
    elif len(floatlist) >= 1 or len(floatlist) == 2:
      return True
      
print is_int(-2)

Check out my code i know that isn’t the best code, but i dont know why is say this:
"Your function fails on is_int(-2). It return None when it Should return True."
I keep prove it and testing and i think that i found a bug or something i dont wanna stay as a smart a** instead i want to know if i made a “good code”. Anyway if someone could answer me quickly i’d appreciate it.


Float and modulo
#2

What matters most at first is knowing what you’re writing, being in control, if it’s a bit long that’s kind of fine for now.

There exists input where your function doesn’t return anything at all, for example the number 123

Another problem is that your function is storing information between calls, so each time you call your function, the all previous data gets remembered and affects the outcome. This is why it fails for -2, it’s the previous calls affecting the outcome. (Also, won’t the negative sign affect the position of the "." ?)

I’m a bit sceptical against looking at the string conversion too, there may be numbers that aren’t integers but get displayed with nothing after the decimal point. It’s probably good enough for this exercise though. Additionally, there are string representations that use completely different notation: 4e+100 … but codecademy won’t test with such numbers so again, probably fine anyway.


#3

Thanks so much for the answer i will try to upgrade my skills and fix this code.
A simple question My function store data because there are variables outside the function?


#4

Hi, just thought of sharing and explaining my answer with you.

def is_int(x): #1
  if float(("%0.2f" % x)) - int(x) > 0 or int(x) - float(("%0.2f" % x)) > 0: #2
    return False #3
  else: #4
    return True #5
  
print is_int(2.04) #6

#1 defining function x, with x as a parameter
#2 the first part of the code

if float(("%0.2f" % x)) - int(x) > 0

<<< this takes care if the number is a positive float or positive integer.

float(("%0.2f" % x))

converts the number to a 2 decimal float.
eg) if x is 2, it will convert x to 2.00 and subtract it from the integer 2 and will check if it greater than zero or not.
the second part of the code following

or int(x) - float(("%0.2f" % x)) > 0:

<<< this takes care if the number is a negative float or a negative integer.
Here we first take the integer and subtract the float (2 decimal value of it).
for eg) if x is -2.4, it will take the integer value of -2.4, which is -2, and subtract the float -2.40 and check if the answer is greater than 0.
#3 this is self explanatory, if the value is #2 is greater than 0, it will return false
#4 else
#5 if the value is not greater than 0, it will return True.
#6 testing function is_int
Hope this helps.


#5

the instructions for this problem are very misrepresented. first the example gives an illustration that says:
def digit_sum( 1234):
should return 10 because 1+2+3+4 = 10
so i understood this illustration as counting 1 + 2 + 3 + 4 not as one thousand two hundred thirty four.
so i tried using iteration or recursive method but the instead we are offered this long and cumbersome solution that does not really make much sense.
my code was as follow (using recursive method) and assuming i want the value of 10 to be return
def digit_sum(x):
if x == 1:
return 1
else:
return x + digit_sum(x-1)
digit_sum(4)


#6

2 posts were split to a new topic: Float and modulo


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.