Python Forum
In input, I want just numbers and not strings!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In input, I want just numbers and not strings!
#1
Hi guys,

I have a simple question for you. Why the "input" function reads always the user input like a string? could not be it a problem?

right now I'm writing a simple script, but before to start, it must check if the user input is a number (int/float) or not. if the user input is a number it's ok, the script can start, otherwise it can't go ahead. How can I do that if Python read all the user inputs like a string? what is the discriminating factor to understand if the user input is a number or not?

p.s.:
I'm not an English speaker and I don't know very well Python, I'm still studying. Currently I'm reading the free book "Think in Python" by Allen B. Downey. I read just the first five chapters.

just to inform you, I'm using Python 3.6.6 in Linux Mint 19 Cinnamon.
Reply
#2
import string,
check if input isdigit:
if entry.isdigit():
    ...
if so convert to integer
Reply
#3
The return type of input is always str. If you expect an integer or a float, use try/except in combination with int()/float().

def input_integer(question):
    while True:
        value = input(question)
        try:
            integer = int(value)
        except ValueError:
            print('Value "{}" is not a valid integer.'.format(value))
            continue
        return integer

def input_float(question):
    while True:
        value = input(question)
        try:
            float_value = float(value)
        except ValueError:
            print('Value "{}" is not a valid float.'.format(value))
            continue
        return float_value

value1 = input_integer('Please enter a valid integer: ')
value2 = input_float('Please enter a valid float: ')
The example functions asking for the value until you enter a valid value.

Checking if the input is an integer is not complicated,
but to check if the str is a valid float is more complex.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,406 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,984 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Adding string numbers, while loop and exit without input. Jose 11 7,449 Apr-15-2020, 08:34 AM
Last Post: Jose
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 8,750 Apr-20-2018, 04:07 AM
Last Post: pythoneer
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,088 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  Sum up numbers using while(strings) RiceGum 7 4,679 Oct-23-2017, 12:41 PM
Last Post: sparkz_alot
  Strings inside other strings - substrings OmarSinno 2 3,648 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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