Python Forum
How to check if user entered string or integer or float??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if user entered string or integer or float??
#1
I am solving a basic example using while loop in which user enters a number and program prints a countdown from that number to zero. But the catch is that program will inform you if user enters an unexpected number or string. For checking if input number in an integer i use //a = isinstance(x, int)//. The program is working only for condition if i enter a negative integer but not executing else statement when i entered float or string.

x=int(input("Enter a positive integer for countdown"))
print(x)
a = isinstance(x, int)
if a==True and x>=0:
    while x!=0:
        x-=1
        print(x)
else:
    print("Please enter a positive integer")
Reply
#2
def main():
    while True:
        x = input("Enter number >> ")
        if x.isdigit():
            if int(x) >= 0:
                break
        elif x.count('.') == 1:
            if x.replace('.','').isdigit():
                print(x)
                break

        print("Please enter a positive integer.")

main()

or
def isint(n):
    try:
        int(n)
        return True
    except:
        return False

def isfloat(n):
    try:
        float(n)
        return True
    except:
        return False

def main():
    while True:
        x = input("Enter number >> ")
        if isint(x):
            if int(x) >= 0:
                print("int", x)
                break
        elif isfloat(x):
                print("float", x)
                break

        print("Please enter a positive integer.")

main()
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Just a side thought, you might want to create a decorator that uses assertions to check your input,
'when you have some spare time', something to pt on the todo list:
https://stackoverflow.com/questions/1529...-arguments
Reply
#4
Don't ask for permission, ask for forgiveness. The try-except is the best example.
Just look how many different representations floats could have:

1.
.0
1e1
1e-1
1e+1
1.1e-1
1.1e+1
.1e+1
nan
inf
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Sorry, i didn't get your point
Reply
#6
(Dec-21-2019, 05:09 PM)xivipa6427 Wrote:
(Feb-26-2018, 05:46 PM)prateek3 Wrote: Sorry, i didn't get your point
userInput = input ("Enter the number")
try:
num = int(userInput)
print("The interger is", num)
except ValueError:
try:
num = float(userInput)
print("The float number is ", num)
except ValueError:
print("It is not a interger or a float")

for more info [URL DELETED]

It's a two year old post, the OP got the answer they needed, and your code isn't in code blocks - was it worth it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 257 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,521 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  convert string to float in list jacklee26 6 1,898 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,852 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,392 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,481 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,329 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,521 May-24-2021, 08:48 AM
Last Post: bowlofred
  converting user input to float troubles RecklessTechGuy 3 2,452 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  Please help me to ask to enter again itself when value entered is not a number. sunil422 5 2,614 Aug-13-2020, 02:15 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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