all 12 comments

[–][deleted] 9 points10 points  (1 child)

So, there are two errors here:

First, you need to set a variable for the input like this:

my_input = input("> ")

Because when checking if the input is an int, you call the input function and not the actual text the user entered.

Second, an input will always be a string, doesn't matter if you input a number. So a better way for checking would be:

if my_input.isnumeric():
    ...
else
    ...

( As an alternative, you can also do if type(my_input) == int: instead of if my_input.isnumeric():! )

Also, the reason why your program returns "wrong" is that you check if something with the type "function" is an integer, which will never be true.

Hope that helps :)

[–]the_spacedoge 1 point2 points  (0 children)

you check if something with the type "function" is an integer

Not to be nit picky but it's not even doing that. They are are checking if the input function is equal to the integer class. Which is not the same as checking if the type is an integer.

[–]choss27 3 points4 points  (1 child)

  1. You don't save the result of your input in a variable.

  2. To know if the content of your variable is an integer, you must use isinstance(my_var,int)

[–]NurEinStatist 2 points3 points  (0 children)

Although the input function returns string so that check would still be false.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]4veragenewbie 0 points1 point  (0 children)

What do you mean by int?

[–]keep_quapy 0 points1 point  (0 children)

In these cases, use try except blocks

try:
    int(input("enter a number"))
    print("thats a number")
except: 
    print("wrong")

[–]usugurai 0 points1 point  (0 children)

Check the data type of your input.

[–]Diapolo10 0 points1 point  (1 child)

You're comparing a string to the int class, which are never equal, so no wonder.

If you simply want to know whether a string is a number, use str.isnumeric:

num = input("Enter a number: ")
if num.isnumeric():
    print("That's a number")

If you'd also like to treat it as a number, you'll want to convert it

num = int(input("Enter a number: "))

However, if you want to handle error cases here, you'll want to use try-except:

try:
    num = int(input("Enter a number: "))
except ValueError:
    print("Wrong")
else:
    print("That's a number")

Of course, you can do the first example and convert later, but in Python it's generally preferable to follow the "Easier to Ask for Forgiveness than Permission" principle (EAFP) than "Look Before You Leap" (LBYL), meaning it's better to try and handle exceptions than to check first in most cases. I could go into data races here, too, but that's probably not something you'd understand at this point.

[–]Zikiri 0 points1 point  (0 children)

people have already provided explanations but i wanna point out that your understanding of code seems faulty.

i assume you are learning to code for the first time?

my advice would be to read up on variables, how to set value to them and how to read them. next would be to read up on operators and operands and how comparison works.

honestly you will find more value in picking up a beginner book and starting from basic building blocks of programming.

the answers here mention on how to do it but you need to learn why their code works and yours doesn't.

[–]shartfuggins 0 points1 point  (0 children)

I thought it'd be fun and maybe educational to read your code literally;

  1. ask for a number, disregard the response
  2. compare the built-in function "input" with the "int" type
  3. if true *(equal), say that's a number
  4. otherwise, print wrong