-1

I've began a very basic program where you have to find the number randomly chosen by the computer. I successfully added the randomizer to the program, yet I couldn't find how to compare the type of the asked number so that a message is printed whenever you type letters instead of numbers for example.

I tried this type of command

if type(nbredevine)!=int:

    print ("""I asked you for an integer. Don't mess with me.""")

unfortunately this isnt working, and even though I've searched for it in the internet and on this site, i haven't been able to find something that fits this special situation.

I haven't been able to make the isinstance function work in this case. Is it because I'm too much of a neophyte ?

Thank you guys, and happy holidays for those of you who are in holidays.

Attack68
  • 4,437
  • 1
  • 20
  • 40
JPRouve
  • 1
  • 1
  • 2
    you want `isinstance(nbredevine, int)`, perhaps you also need to make sure the user input is not a string too: the `input()` function will by default give any user input as a string – jeremycg Dec 24 '19 at 18:26
  • 1
    Yes, Python can detect how new you are, and disables certain advanced features, like type comparison /s – Mad Physicist Dec 24 '19 at 18:27
  • 1
    Chances are that you actually have the input as a string. If you want to see if it looks like an integer, try and cast it to `int` and see if it errors. – khelwood Dec 24 '19 at 18:28
  • 1
    Seriously though, your input will never be an integer. Anything you type will be a string, even if it contains digits. – Mad Physicist Dec 24 '19 at 18:28
  • @MadPhysicist I was supposing that i was misusing the command, not that python would block the function. – JPRouve Dec 24 '19 at 18:30
  • @jeremycg so how can i change the type of the input() command ? thank you for your response. – JPRouve Dec 24 '19 at 18:33
  • So, to elaborate on what @MadPhysicist is saying, `'12'` is *always* a `str` object, whereas `12` is an `int` object. That `'12'`, the `str`, happens to contain text which would represent an `int` doesn't matter, it is still a `str`. if you are using `input()` then the result will always be `str` type. – juanpa.arrivillaga Dec 24 '19 at 18:33
  • The problem is that my program prints this whenever I type some letters in the program's nbredevine var Traceback (most recent call last): File "python.py", line 8, in nbredevine=input("""Try to guess the number randomly chosen by the computer (between 0 and 100) :""") File "", line 1, in NameError: name 'test' is not defined – JPRouve Dec 24 '19 at 18:40
  • @wwii I basically started to program today, years after doing basic programs like this in C. I don't understand the code of the page you sent me. Could any of you possibly explain it to me ? I just wanna understand, I don't want to bother anyone – JPRouve Dec 24 '19 at 18:42
  • Are you using Python 2? That would explain the error. Please **always** provide a [mcve], which is a requirement for these sorts of questions. In any case, you definitely shouldn't be learning Python 2, which is essentially at its official end of life (Jan 1, 2020) – juanpa.arrivillaga Dec 24 '19 at 19:05

1 Answers1

0

When you use input, you always get a string. If the string contains only digits, it can be converted to an integer, but it's still a string. There are a couple of things you can do.

If you are only interested in non-negative integers, you can check using str.isdigit:

if nbredevine.isdigit():
    value = int(nbredevine)
    # use integer value
else:
    print('Not an integer')

If you want to bypass all the fluff and check directly, you can just try converting to an integer:

try:
    value = int(nbredevine)
except ValueError:
    print('Not an integer')
else:
    # use integer value
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Thank you very much for your answer. Unfortunately, I still have the same error. Here is the error message i get : Traceback (most recent call last): File "python.py", line 8, in nbredevine=input("""Try to guess the number randomly chosen by the computer (between 0 and 100) :""") File "", line 1, in NameError: name 'test' is not defined – JPRouve Dec 24 '19 at 19:01
  • @JPRouve. That error has nothing to do with the posted code. You're trying to reference an undefined variable named `test` somewhere. – Mad Physicist Dec 24 '19 at 19:14
  • Well, it is because i try to input "test" inside of my nbredevine var – JPRouve Dec 25 '19 at 09:44
  • @JPRouve. That's not what the error says, unless you forgot quotes around `test`. FWIW, you can select an answer even after your question gets closed. – Mad Physicist Dec 25 '19 at 09:58