how do i get this right (python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how do i get this right (python)

name =input("Enter a name ") if name is int: print ("that is number") else name is str: print("Hi " + name) what i'm trying to is user enter a name but if user put int or number i wanna get a reply saying that's a number and keep looping until user put name . so how do i get it right?

16th Dec 2016, 4:09 PM
perweez neqqi
perweez neqqi - avatar
5 Answers
+ 4
while(True): name = input("Enter a name ") try: x = int(name) x+=1 print("\n" + name + " is a number") except: break print("\nHi " + name) Try this↑: it tries to convert and increment the input. If that fails, the input must be a string. You can use float() instead of int() to check for inputs like 1.23.
16th Dec 2016, 6:31 PM
Jafca
Jafca - avatar
+ 2
Not sure how to do it in python exactly but the logic behind this would be: name = input("Enter a name ") while (name is int) print("That is a number") name = input("Enter a name ") print("Hi " + name)
16th Dec 2016, 5:23 PM
Jafca
Jafca - avatar
+ 2
Glad I could help :) #+=1 can't be converted to an int so it stays as a string. If you want a name with no numbers in, you could create a boolean method to check every letter of name, and put it in an if statement.
17th Dec 2016, 9:58 PM
Jafca
Jafca - avatar
0
Hi Jafca thx for helping but i got this from your code >>>enter a name 123 >>> Hi 123 its suppose to print out >>>"This is a number"
16th Dec 2016, 6:07 PM
perweez neqqi
perweez neqqi - avatar
0
Hi jafca thx for the help you got it right , it work but #+=1 don't needed it still work without it but hey tanks man :) .
17th Dec 2016, 5:16 PM
perweez neqqi
perweez neqqi - avatar