Recalling function (recursion) versus using while statement in Python

Well, I know that you can run the program as long as a certain condition is true using the while statement . However, is it wrong or bad practice to just call a function in else , as shown below?

def ask():
    me = input("What is your name? ")

    if me == "Tom":
        print("Hi, Tom!")
    else:
        print ("Who are you?")
        ask()

This seems like a lighter, shorter version of the “while statement”, but in fact I have not seen a program like this in Python tutorials.

+3
source share
4 answers

, "", . , - , . , , ( ) while. ( ?)

+3
+1

, ? :

def ask():
    while input("What is your name? ") != "Tom":
        print ("Who are you?")
    print("Hi, Tom!")
0

while, , ask , .

, , :

def ask(count):
    if count < 0:
        return False

    me = input("What is your name? ")

    if me == "Tom":
        print("Hi, Tom!")
        return True
    else:
        print ("Who are you?")
        return ask(count - 1)

?

ask while, . 'while' 'for', return.

0

All Articles