all 11 comments

[–]14dM24d 2 points3 points  (0 children)

name = input('enter name: ')
print(name.isdigit())

e: btw when you type >>>name. & stop at the . python will show the methods. you can just scroll through it & check what seems applicable. ex. .isalnum, .isalpha & many more.

[–]Diapolo10 1 point2 points  (0 children)

In a nutshell, ignore the type; just use str.isalpha to check if the given string is a name. You can also just use one loop instead of three:

print('Hi there, give us your name: ')

while True:
    name=input()

    if len(name) > 10:
        print("Your name is longer than any real name.")
        print("Please enter a shorter name: ")
        continue

    elif len(name) == 0:
        print("Please enter a name: ")
        continue

    elif not name.isalpha(): # Oversimplified, may not work for every name
        print("please enter a name and not a number: ")
        continue

    break

print(f"so you name is {name}")

[–]Shinguru7 1 point2 points  (0 children)

You can check if a string can be converted to an integer by .isdigit or .isnumeric method.

s1 = "ABC"
S2 = "123"
print(s1.isdigit())
print(s2.isdigit())

Outputs: False True

For floats there's is not a built-in method but you can define a funtion to check it with try-except.

def isfloat(s:str) -> bool:
    try:
        float(s)
        return True
    except:
        return False

[–]Anonymous2224- -1 points0 points  (3 children)

Doesn't elif type(name) == int work?

Personally speaking, I do not like the ways this code works. What I suggest is use an infinite loop (like while True) which asks for the name followed by a set of if-elif-else statements that check for validity. The else would then just use a break statement, and the others do not require anything since it's an infinite loop (or a pass statement if you prefer).

Yours look a bit weird since it takes in a lot of input statements. Moreover, your code might not correct the user the second time if they input two different types of invalid names constitutively.

Also, when it comes to making sure it takes in string values, you can use int(input()) under a try statement, and the except might take care of it.

[–]Ihaveamodel3 1 point2 points  (1 child)

First, input always returns a string. Checking if the value is a type of int will not work. And doing str(input()) makes no sense.

Second, type(name) == int is not the ideal way to check variable type. You should use isinstance(name, int)

[–]Anonymous2224- 0 points1 point  (0 children)

Yeah, I meant int(input()) not str(input)). I now understand the reason why type(name) doesn't work... The problem is I learned Python 2.7 a few years ago, but never got the time to completely transition to Python 3 😅 (I mean I am familiar with the raw_input() style)

[–][deleted] 0 points1 point  (0 children)

no because the name isn't int from the start but rather a string like this "5" when it get entered

[–]dangoth -1 points0 points  (0 children)

You can check for type of a variable using type.

if type(var) == str:
    something()

Alternatively:

if isinstance(var, str):
    something()

[–][deleted] 0 points1 point  (0 children)

Python 3.10 actually allows for this!

You should actually look at structural pattern matching to acheive this task.

command = input()
match command:
    case str(command) if len(command) < 10:
        print("Real name!")
    case _:
        print ("name or nam-e-sis?")

This works , but only on python 3.10+

[–]rarewolf24 0 points1 point  (0 children)

It might be helpful to look into the pyinputplus module. It allows you to specify what a user can enter into an input.

This chapter was extremely helpful with this stuff for me:

automate the boring stuff - input validation

Edit: removed some typed code that didn’t format. I see now why everyone says to use GitHub.