Converting a input() to an int

def is_it_integer(prompt):
    n = prompt
    try:
       int(n) == n
       prompt = True
       return n
    except ValueError:
        print("error.")
        prompt = False
        return prompt

count = 0

first_guess = input("Enter a number..:")


if is_it_integer(first_guess):
    first_guess = int(first_guess)

if count < first_guess:
    print("GO TO BED IT WORKED!")

Input these 3 characters before your code (```), left to 1 on my keyboard, but don’t include the brackets :smiley_cat: Hope this helps

1 Like

tried it, no luck. But thank you.

nevermind, totally worked, was using ‘’’, instead of ```

Nice, also you can do this: [code] … [/code] around the code.

2 Likes

Two things:

  1. int(n) == n is how you convert an int, however this line doesn’t really do anything. You would need to assign the result to something. x = int(n) == n
  2. You can really just do: return int(n) and ditch everything inside the try: before the except: If you attempt to convert something not an int it will throw the except, so then in your except: just return False.
1 Like

That looks so much better, and I get it now. Thanks!!!

I’m ignorant here because something I thought would work, doesn’t. I was wondering why we couldn’t just use the type() function to do something like this

if type(x) is int(1):
   print('x is an int')

but it fails, with either == or is.. 
>>> x = 1
>>> type(x) == int(1)
False
>>> type(x)
<class 'int'>
>>> type(int(1))
<class 'int'>
>>> type(x) is int(1)
False

why?

Try this:

>>> type(10) is int
True
>>> type("10") is int
False
>>>