Mar-28-2019, 08:59 AM
(Mar-28-2019, 08:31 AM)DeaD_EyE Wrote: If you want to have integers, just convert the input in a try-block and let theint()
function raise a ValueError. Catch this ValueError and continue. If there was no ValueError, you got your result. In this case the else-block is executed and you can break out of the while true loop.
No matter what you put into theint()
function, the function converts only valid integer.
If it's not valid, the function raises an ValueError.
My line of thought was more towards this: if you validate input with string length and checking whether all characters in string are numeric then inputs starting with zero(s) will pass. If you later need to perform calculations with integer of specific length it may be a problem (instead of hundred-millions you may have 1), if it will be kept as string it doesn't matter.
str.isnumeric/isdigit validation has also this behaviour which must be also considered while taking user input:
>>> user_input = '1234567890²' >>> len(user_input) 11 >>> user_input.isnumeric() True >>> user_input.isdigit() True >>> int(user_input) /../ ValueError: invalid literal for int() with base 10: '1234567890²'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.