mi50hw
mi50hw

Reputation: 13

Using isaplha() and while loop with if condition

I am still learning python programming. The program that I am trying to do will get the user's name and age. Using while loop, I need to let the user enter again if the user did not enter a numeric value for age. However, it did not give the output that I want. I tried using isalpha() but, I think I am doing it wrong.

For example, if I input a name "John" and age "d", it will let the user input again. However, if I input a name "John" and age "6", the program will loop and ask the name and age again even though the age is a numerical.

This is my code snippet.

def getName():
b = input("Enter your name: ")
c = input("Enter your age: ")
return b, c

def dispName():
x, y = getName()
while y.isalpha() == True:
    getName()
    if y.isalpha() == False:
        print("Your name is: ")
        print("Your age is ")
        break

dispName()

How can I use the while loop properly to achieve my desired output?

Upvotes: 1

Views: 621

Answers (1)

user9368809
user9368809

Reputation:

def getName():
    b = input("Enter your name: ")
    c = input("Enter your age: ")
    return b, c

def dispName():
    while(True):
        x,y = getName()
        if not y.isalpha():
            break
    print("Your name is: "+x)
    print("Your age is "+y)

dispName()

Upvotes: 1

Related Questions