Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
blank space + input()
#6
(Mar-05-2019, 05:38 PM)ClassicalSoul Wrote: What alternative would you recommend?

You can use isdigit(), but that will be False for '-1', which will convert to an integer. If you only want positive numbers, that's not a problem. If you want to include negatives, the conditional becomes a little messy: if text.isdigit() or (text[0] == '-' and text[1:].isdigit()):. At that point I would just go with try except:

try:
    print(int(x) + 2)
except ValueError:
    if x.lower() == 'quit':
        break
    else:
        print('Integers only, please.')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
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,357 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Verify input as integer and not blank GMCobraz 3 2,168 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 2,825 Jan-06-2020, 08:40 AM
Last Post: buran
  How to recognize space or enter as one-character input? Mark17 5 5,694 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