Python Forum
need help with making a validating function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help with making a validating function
#7
(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 the int() 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 the int() 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.
Reply


Messages In This Thread
RE: need help with making a validating function - by perfringo - Mar-28-2019, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] formula for validating monetary values? kakos_k9 1 855 Dec-17-2022, 09:28 PM
Last Post: woooee
  Making a function more efficient CatorCanulis 9 2,091 Oct-06-2022, 07:47 AM
Last Post: DPaul
Question Making a copy list in a function RuyCab 1 1,884 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Making a code.py file to function, does not run hobbyist 6 3,064 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Validating user input WJSwan 2 2,230 Jul-06-2020, 07:21 AM
Last Post: menator01
  making a function that writes variables (is possible?) miker2808 3 2,445 Jan-30-2020, 06:27 PM
Last Post: buran
  Validating the functionality of the application rpalakodety 1 1,854 Dec-30-2019, 07:58 PM
Last Post: ndc85430
  Validating information from .csv file before executemany mzmingle 7 4,728 Apr-15-2019, 01:40 PM
Last Post: mzmingle
  Base for making a search function SheeppOSU 1 2,385 Mar-19-2019, 11:37 PM
Last Post: Yoriz
  Making list empty after return in function dan789 10 6,016 Nov-24-2018, 11:54 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020