If, elif and else - ask only for strings, continue the loop if the user enters a number

Viewed 203

I have been wondering this for a longer time If I check for user input, I know how to check for strings. But what if I want to check if the user has entered just integers or floats. I tried it with an "if name == int or float: block but of course that didn't work. Can you help me out? How do I check for that? Here is my code. Of course the input I always converted to a string so I don't know how to do that.

print("Enter a name: ")
while True:
    name = input()
    if name != "":
        print("Thank you for entering a name: ")
        break
    else:
        print("Please enter a name: ")

I also tried this:

print("Enter a name: ")
while True:
    name = input()
    if type(name) != int:
        print("Thats not a name!")
        print("Now please enter a name without numbers")
    elif type(name) != float:
        print("Thats also not a name!")
        print("Now please enter a name without numbers")
    elif type(name) != "":
        print("Thank you for entering your name!")
        break
    else:
        print("Please enter a name: ")
3 Answers

Edit: This might help.

def hello(userinput):
    if userinput.isdigit():
        print("You entered a number. That's not valid!")
    else:
        print("You entered a valid name.")

Old Solution

print("Enter a name: ")
while True:
    name = input()
    if type(name) != int:
        print("Thank you for entering a name: ")
        break
    else:
        print("Please enter a name: ")

Also I can understand why you did that. The previous code you created meant that if the user typed the class int. This will be impossible to type a class because input() only takes a string from the user's input.

For int you can use the isnumeric() method

"1".isnumeric() #True

For float, you should try if casting to float is possible

 def isFloat(num):
    try:
        float(num)
        return True
    except ValueError:
        return False

Now use both these methods :

print("Enter a name: ")
while True:
    name = input()
    if name.isnumeric() or isFloat(name):
        print("Thats not a name!")
        print("Now please enter a name without numbers")
    elif type(name) != "":
        print("Thank you for entering your name!")
        break
    else:
        print("Please enter a name: ")

Output :

Enter a name: 
1
Thats not a name!
Now please enter a name without numbers
1.2
Thats not a name!
Now please enter a name without numbers
abc
Thank you for entering your name!

To check if a variable is of a type, simply use if type(VARIABLE) == TYPE: where "VARIABLE" is your variable and TYPE is your type (such as int, float, str, etc.).

In your code, however, you may want to use str.isnumeric() as it will return True when there are numerical values in the string. To check if ANY characters are numeric, do if all([not char.isnumeric() for char in list(STRING)]): where "STRING" is your user's input. This checks if every character in the string is not numeric and returns True when this is the case.

Related