Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input check help!
#1
Hello, i am new to Python so i couldn't figure out how to check the input.

I want to get an input from the user(integer or float) and want to give an error message when it enters anything else(letter or any special character).

So far i know this works for the integer:

while True :    

    try:
        x = int(input('please enter an integer: '))
        print(x)
        break
    
    except ValueError: 
       print('not valid')
First can someone please explain the logic of this. I didn't understand it thoroughly.

But if i want it to accept decimal numbers as well integers what should i write? (very basically cause i am new)

Here are some of the things i tried which none of them worked.
1)
radius= input("enter a number: ")
if isinstance(radius,float): 
    print(radius)
elif isinstance (radius,int):
    print(radius)
else:    
    print("Not a number")
2)
radius= input("enter number: ")
if isinstance(radius,numbers.Real): 
    print(radius)
    else:
        print("not number")
3)
radius= input("enter number: ")
  if type(radius) in (float,int):
    print(radius)
  else:
    print("not a number")
4)
x=input('please enter integer: ')
if type(x) is int:
    print(x)

elif type(x) is float:
    print(x)
    
else:
    print('error')
5)
x=input('please enter integer: ')

if not type(x) is int: 
    raise TypeError('only numbers allowed')
Reply
#2
Instead of x=int(...) (which tries to interpret the input string as an integer), you could just do x=float(...). That will accept integers as well as floating-point numbers.
Reply
#3
Thanks! for the first one it works, but what is wrong with the others?
Reply
#4
Whenever you input something like

x = input()
Then the variable (x in this case) is always a string. In some of your other examples you're asking if it's a float, or an instance of a float, etc. It will never be. The contents might look like an integer, but the variable remains a string. You can attempt to cast that value it to a int or something else:

x = input()
i = int(x)
But of course that can fail if the input text has characters in it. What your first one does is try to cast it this way, and print a message if that fails.

x = input()
try:
    i = int(x)
    [do stuff here with your int...]
except ValueError:
    # Ooops, something in the try section failed with a valueerror.  That likely
    # means the string didn't look like an int.  We can complain here, and maybe
    # loop to retry.
    print(f"The input {x} doesn't seem to be a number. I don't know what to do with it.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 389 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,772 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Check all input, easy way! Help pls! bntayfur 2 1,779 Jul-05-2020, 10:58 PM
Last Post: bntayfur
  how i can check the input type? Firdaos 3 2,844 Dec-13-2018, 11:39 PM
Last Post: wavic
  Validating Input (basic check for int etc) gruntfutuk 1 2,484 Aug-06-2018, 07:43 AM
Last Post: gruntfutuk
  check if input is correct before proceeding Pedroski55 1 3,127 Sep-16-2017, 01:56 AM
Last Post: rdx

Forum Jump:

User Panel Messages

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