Help in Understanding How Variables Update /u/WillingnessPast5294 Python Education

So I was making some test system and came across with this problem.

Here’s the code:

name = '' def username(name): print('What is your name?n') name = input().title() print('Is this correct?n', name, 'nY/N') response = input().upper() if response == 'Y': pass elif response == 'N': name = input().title() username(name) print(name) 

The problem was, it wasn’t printing the updated value(the name) but the first value. Then I found the solution which is this:

def username(): print('What is your name?n') name = input().title() print('Is this correct?n', name, 'nY/N') response = input().upper() if response == 'Y': return name elif response == 'N': name = input().title() return name name = username() print(name) 

Why did this work and not the other one? I inputted the variable inside the function and returned the value, so it should’ve had printed it. Does functions not affect variables outside of it even if it the variable is put in as an argument of the function? Does the variable not update if I don’t use the ‘global’ variable?

submitted by /u/WillingnessPast5294
[link] [comments]

​r/learnpython So I was making some test system and came across with this problem. Here’s the code: name = ” def username(name): print(‘What is your name?n’) name = input().title() print(‘Is this correct?n’, name, ‘nY/N’) response = input().upper() if response == ‘Y’: pass elif response == ‘N’: name = input().title() username(name) print(name) The problem was, it wasn’t printing the updated value(the name) but the first value. Then I found the solution which is this: def username(): print(‘What is your name?n’) name = input().title() print(‘Is this correct?n’, name, ‘nY/N’) response = input().upper() if response == ‘Y’: return name elif response == ‘N’: name = input().title() return name name = username() print(name) Why did this work and not the other one? I inputted the variable inside the function and returned the value, so it should’ve had printed it. Does functions not affect variables outside of it even if it the variable is put in as an argument of the function? Does the variable not update if I don’t use the ‘global’ variable? submitted by /u/WillingnessPast5294 [link] [comments] 

So I was making some test system and came across with this problem.

Here’s the code:

name = '' def username(name): print('What is your name?n') name = input().title() print('Is this correct?n', name, 'nY/N') response = input().upper() if response == 'Y': pass elif response == 'N': name = input().title() username(name) print(name) 

The problem was, it wasn’t printing the updated value(the name) but the first value. Then I found the solution which is this:

def username(): print('What is your name?n') name = input().title() print('Is this correct?n', name, 'nY/N') response = input().upper() if response == 'Y': return name elif response == 'N': name = input().title() return name name = username() print(name) 

Why did this work and not the other one? I inputted the variable inside the function and returned the value, so it should’ve had printed it. Does functions not affect variables outside of it even if it the variable is put in as an argument of the function? Does the variable not update if I don’t use the ‘global’ variable?

submitted by /u/WillingnessPast5294
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *