0

So I want to ask the user how old he is in Python. I want to give an assertion error when the input is 0 or lower and when the input is not an integer, so for example the user types something like a comma or something else. This is what I have now

test = input("How old are you?: ")
assert test > 0 and type(test) == int, "Must be more than 0 and only numbers"

And this doesn't work, because when the user types in an integer like 15, it is considered a string and > is not allowed between a string and an integer. So I tried to make the input an integer, like this:

test = int(input("How old are you?: "))
assert test > 0 and type(test) == int, "Must be more than 0 and only numbers"

This above works for the first assert. When the user puts as input -1 or 0, it gets an assertion error. However, it does not work for the second assert, because the type is already an integer. How can I solve this?

  • Does this answer your question? [How can I check if a string represents an int, without using try/except?](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except) –  Nov 12 '20 at 14:05
  • @Yan I still don't get it, do I need to write a function? – vivalasana Nov 12 '20 at 14:08
  • With positive integers you could use `.isdigit`, it works with positive integers; it is appropriate here because you need only integers greater than 0. –  Nov 12 '20 at 14:10
  • @Yan I saw that indeed at the post but when I do assert test.isdigit and type(test) == int, it gives me an assertion error when the user says 5 – vivalasana Nov 12 '20 at 14:18
  • Sorry, I didn't know. Maybe I'll try to find another solution to this question. –  Nov 12 '20 at 14:19
  • @Yan Thanks for helping out, I'll try again with the isdigit function, maybe I did something wrong – vivalasana Nov 12 '20 at 14:20
  • Does https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers help? – Karl Knechtel Nov 12 '20 at 14:30

3 Answers3

0

In this example, test will always be an int or there will be an error, because Python won't be able to convert the input into an int.

You can do it like this:

test = input("How old are you?: ")
assert test.isdigit(), "Must be a number"
assert int(test) > 0, "Must be more than 0"

But in Python we usually solve problems like this with exceptions, consider this example:

try:
    test = int(input("How old are you?: "))
except ValueError:
    print("Must be a number")

if test <= 0:
    print("Must be more than 0")
JanLikar
  • 1,296
  • 9
  • 22
0

int(',') will already produce a ValueError, so you could catch that and split it up like this:

try:
    test = int(input("How old are you?: "))
    assert test > 0, "Must be more than 0"
except ValueError:
    print("only numbers")

This also follows the EAFP idiom.

0
try:
    val = int(user_input)
    print("Input is an integer number. Number = ", val)
except ValueError:
    try:
        val = float(user_input)
        print("Input is a float  number. Number = ", val)
    except ValueError:
        print("No.. input is not a number. It's a string")

Use something like this...

DuB
  • 1
  • 3