0

I'm trying to set up a function that has the user input their name and then confirm their name is correct before continuing. If the name is not correct I want them to be able to go back in and change their name. but I can't figure out how to return the name to the global. I've played around with what seems like a million different ways, but these are the two most recent ways I've tried....

def assign_name():
    name = input("What is your name? ")
    confirm = input(f"is {name} correct?  Y/N")
    if confirm != 'y':
        assign_name()
    else:
        print(name)  ### tester
        return name


my_name = assign_name()
print(my_name)  ### tester


################################

def assign_name(name):
    confirm = input(f"is {name} your name?  Y/N")
    if confirm != 'y':
        assign_name(input("What is your name?"))
    else: 
        print(name)  ### tester
        return(name)



my_name = assign_name(input("What is your name?"))
Healfdene
  • 3
  • 2
  • What do you mean "return to global"? – ybl Feb 27 '20 at 05:56
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Peter Wood Feb 27 '20 at 05:57
  • Samaksh - i want the name that is entered to be stored in the my_name variable. but if I print the my_name after running it through i only get "None" – Healfdene Feb 27 '20 at 05:57
  • Peter - Unfortunately it has not. I've read through that post already and couldn't see how any of it helped my situation – Healfdene Feb 27 '20 at 06:00
  • @Healfdene https://repl.it/repls/JointTerribleVirtualmachines your code works as it is, maybe you are doing something else? – ybl Feb 27 '20 at 06:01
  • @Healfdene after ```if confirm != 'y': ``` return your function call .i.e ```return assign_name() ``` , you are not retuning in if stattement obiviously it will return None by default – vkSinha Feb 27 '20 at 06:01

4 Answers4

1

Here's a fixed implementation using the recursive approach you've attempted:

def assign_name():
    name = input("What is your name? ")
    confirm = input(f"Is {name} correct? (Y/N) ")
    if confirm.lower() != 'y':
        return assign_name()
    return name

my_name = assign_name()
print(my_name)

Output:

$ python3 test_recursion.py
What is your name? first
Is first correct? (Y/N) n
What is your name? second
Is second correct? (Y/N) y
second

However, recursion isn't an efficient approach in this case. It more efficient to use a while loop and not incur the overhead of repeated function calls and variable instantiation.

Here's an alternative approach:

def assign_name():
    while True:
        name = input("What is your name? ")
        confirm = input(f"Is {name} correct? (Y/N) ")
        if confirm.lower() == 'y':
            break
    return name

my_name = assign_name()
print(my_name)

Output:

$ python3 test_loop.py
What is your name? first
Is first correct? (Y/N) n
What is your name? second
Is second correct? (Y/N) y
second

Regarding your original implementation attempt:

You do not return the result of the recursive function call. Since your return statement is within the else block, no return statement is ever called for your initial invocation of the function. That's why None is returned.

If your return statement was not within the else block, it would still be problematic as you would end up returning the initial value provided, essentially disregarding the work performed via recursion.

chuckx
  • 6,484
  • 1
  • 22
  • 23
0

Here is one simple way to go about it. When it comes to getting variables passed around between functions you have three options:

  1. Use global statement inside your functions (a lot of people will flame you for it.....) 2.Pass around functions as the args to other functions (makes a fun game) 3.Use classes

    def assignName():
      name = input("Whats your name?")
      while not checkName(name):
        assignName()
    
      return name
    
    def checkName(name):
      if input(f"Is {name} correct?"): return name
    
    name = assignName()    
    
OneLiner
  • 571
  • 2
  • 6
0
def assign_name():
    name = input("What is your name? ")
    confirm = input(f"is {name} correct?  y/n ")
    if confirm != 'y':
        name = input("What is your name? ")
    else:
        name = name  ### tester
        print(name)
        return name


my_name = assign_name()
print(my_name, "name") 
pri
  • 104
  • 1
  • 8
0

The code fails to assign the new user input to name. Try this:

def assign_name():
    name = input('What is your name? ')
    confirm = input(f'is {name} correct? Y/N')
    if confirm != 'Y':
        name = assign_name()
    return name

There is an issue with the above solution: recursion depth. I doubt it would happen in normal use of this function, but it's good to note that a frame is added to the stack each time the user resubmits a name. Eventually the program will exceed the maximum stack size or the available memory. Also, be certain to maintain consistent case or use a case-insensitive comparison, i.e. 'Y' != 'y'

Give it the old while-loop treatment in order to avoid stack height problems:

def assign_name():
    is_correct = False
    while not is_correct:
        name = input('What is your name? ')
        is_correct = input(f'is {name} correct? Y/N') == 'Y'
    return name
Michael Ruth
  • 2,938
  • 1
  • 20
  • 27