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


Messages In This Thread
input check help! - by bntayfur - Jun-02-2020, 08:06 PM
RE: input check help! - by bowlofred - Jun-02-2020, 08:51 PM
RE: input check help! - by bntayfur - Jun-02-2020, 09:27 PM
RE: input check help! - by bowlofred - Jun-02-2020, 11:38 PM

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 603 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,897 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Check all input, easy way! Help pls! bntayfur 2 1,874 Jul-05-2020, 10:58 PM
Last Post: bntayfur
  how i can check the input type? Firdaos 3 2,932 Dec-13-2018, 11:39 PM
Last Post: wavic
  Validating Input (basic check for int etc) gruntfutuk 1 2,547 Aug-06-2018, 07:43 AM
Last Post: gruntfutuk
  check if input is correct before proceeding Pedroski55 1 3,201 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