Python Forum

Full Version: blank space + input()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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):
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')
Also, text = input('\nPlease enter a number (or \'quit\' to exit):').
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²'
(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?
(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.')
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