Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
blank space + input()
#1
Hi, how do make it so that a blank space precedes an input() function, and even more specifically, how would I make it so that this applies only to the final line?

text = ''
while text != 'quit':
    text = input('Please enter a number (or \'quit\' to exit):')
    if text == 'quit':
        print('...exiting program')
    elif text.isnumeric() == True:
            print(x + 2)
    elif text.isnumeric() == False:
        print('That is not a number.', end = '')
What I want to fix (look how it's all smudged together):
Please enter a number (or 'quit' to exit):j
That is not a number.Please enter a number (or 'quit' to exit):
Reply
#2
You don't need to use: elif text.isnumeric() == True: use elif text.isnumeric(): instead
while True:
    x = input("Enter a number or 'quit' when finished: ")
    
    if x.isnumeric():
        print(int(x) + 2)
    else:
        if x.lower() == 'quit':
            break
        else:
            print('Not a number, try again')
Reply
#3
Also, text = input('\nPlease enter a number (or \'quit\' to exit):').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
One word of caution about using str.isnumeric. Documentation states:" Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric."

In real life scenarios .isnumeric() is not sufficient to determine whether user input can be converted to int or not. Following is expected behaviour:

>>> '1234567890²'.isnumeric()
True
>>> int('1234567890²') + 2
/../
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
#5
(Mar-05-2019, 11:21 AM)perfringo Wrote: One word of caution about using str.isnumeric. Documentation states:" Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric." In real life scenarios .isnumeric() is not sufficient to determine whether user input can be converted to int or not. Following is expected behaviour:
 >>> '1234567890²'.isnumeric() True >>> int('1234567890²') + 2 /../ ValueError: invalid literal for int() with base 10: '1234567890²' 
What alternative would you recommend?
Reply
#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
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  from global space to local space Skaperen 4 2,269 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Verify input as integer and not blank GMCobraz 3 2,085 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 2,740 Jan-06-2020, 08:40 AM
Last Post: buran
  How to recognize space or enter as one-character input? Mark17 5 5,498 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