Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
blank space + input()
#7
I agree with ichabod801 that try..except is way to go. It can be wrapped into function to separate what from how.

If it's about validating user input then one possibility is (without 'quit' part, it's unclear what function should return in that case):

>>> def validate_integer(request):
...     while True:
...         answer = input(request)
...         try:
...             return int(answer)
...         except ValueError:
...             print(f'Expected integer but got {answer}')
...
>>> validate_integer('Please enter integer: ')
Please enter integer: a
Expected integer but got a
Please enter integer: -1
-1    
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
blank space + input() - by ClassicalSoul - Mar-05-2019, 02:15 AM
RE: blank space + input() - by Larz60+ - Mar-05-2019, 02:39 AM
RE: blank space + input() - by ichabod801 - Mar-05-2019, 02:41 AM
RE: blank space + input() - by perfringo - Mar-05-2019, 11:21 AM
RE: blank space + input() - by ClassicalSoul - Mar-05-2019, 05:38 PM
RE: blank space + input() - by ichabod801 - Mar-05-2019, 06:39 PM
RE: blank space + input() - by perfringo - Mar-06-2019, 09:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  from global space to local space Skaperen 4 2,338 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Verify input as integer and not blank GMCobraz 3 2,154 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 2,811 Jan-06-2020, 08:40 AM
Last Post: buran
  How to recognize space or enter as one-character input? Mark17 5 5,659 Oct-17-2019, 08:19 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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