Bottom Page

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):
Quote
#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')
Quote
#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

Quote
#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²'
ClassicalSoul likes this post
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.
Quote
#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?
Quote
#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

Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to recognize space or enter as one-character input? Mark17 5 148 Oct-17-2019, 08:19 PM
Last Post: jefsummers
  Pygsheets Keeps inserting a blank row, Need Help HiImNew 1 947 Jan-26-2018, 08:48 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)