-2

I'm trying to ask the user to submit a positive integer because my question is "How old are you?"

1 Answers1

2

Use string.isdigit().

str.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise.

For 8-bit strings, this method is locale-dependent.

>>> '-23'.isdigit()
False
>>> '23'.isdigit()
True
>>> '23.45'.isdigit()
False

So it would be like,

>>> while True:
        s = input('How old are you: ')
        if s.isdigit():
            break

    
How old are you: y
How old are you: -7
How old are you: 8.9
How old are you: 8
>>> 
Community
  • 1
  • 1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274