1

So the below code works fine:

numOfPasswordsGiven = input("How many passwords should this program generate? ")
lengthOfPasswordsGiven = input("How long should these passwords be? ")

print(numOfPasswordsGiven)
print(lengthOfPasswordsGiven)

But the code below doesn't:

def testing () :

  numOfPasswordsGiven = input("How many passwords should this program generate? ")
  lengthOfPasswordsGiven = input("How long should these passwords be? ")

testing()
print(numOfPasswordsGiven)
print(lengthOfPasswordsGiven)

When I run this second snippet of code, I get the following error:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    print(numOfPasswordsGiven)
NameError: name 'numOfPasswordsGiven' is not defined

I've debugged the program. It looks like lengthOfPasswordsGiven isn't being passed the value that I entered when prompted, in the second code snippet. But the value is getting passed to this variable in the first code snippet.

I've also tried putting the input statements in two different definitions. The same error occurs.

How do I fix this?

2 Answers2

3

You isolated the scope of those two variables to within that method in your second example, as opposed to your first example where you are in the same global scope. That explains why you are seeing this behaviour.

Take a look at the documentation on scoping here:

https://docs.python.org/3.8/reference/executionmodel.html#resolution-of-names

If you want to access them outside of your method, you will need to return them. It will end up being returned to you as a tuple by using the following modified version of your code:

In [1]: def testing () :
   ...:
   ...:   numOfPasswordsGiven = input("How many passwords should this program generate? ")
   ...:   lengthOfPasswordsGiven = input("How long should these passwords be? ")
   ...:
   ...:   return numOfPasswordsGiven, lengthOfPasswordsGiven
   ...:
   ...: passwords_given, passwords_given_length = testing()
   ...: print(passwords_given)
   ...: print(passwords_given_length)
How many passwords should this program generate? two
How long should these passwords be? fourty two
two
fourty two
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 1
    Thanks idjaw! Looks like adding a **global** keyword with the two variables following also fixes this too. But I found out just now that [using the global keyword should be avoided](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil). – justAnotherNoob Apr 08 '20 at 21:34
  • 1
    Just in case you didn't get my comment @idjaw. I want folks to know how much I appreciate them for answering my questions. Be safe! – justAnotherNoob Apr 09 '20 at 21:44
  • @NeoSoulPanther Hey. You're very welcome! I'm glad this helped! Happy coding. – idjaw Apr 09 '20 at 21:47
1

This has to do with the scope of the variables. Here is a good explanation of what that means.

  • Thanks for the link Reid Pritchard! I'll bookmarked python-textbok.readthedocs.io so that I could use it as a future reference. – justAnotherNoob Apr 08 '20 at 21:35
  • just wanted to thank you again for the link! I read the page just now. The paragraphs beginning with "By default, the assignment statement creates...", "Because we haven’t declared...", and "Note that it is usually very bad..." were particularly useful. – justAnotherNoob Apr 09 '20 at 21:43
  • @NeoSoulPanther No problem! Variable scope can be tricky. Getting a good understanding of it early will help you a ton in the future! A great example of scope's importance is javascript's 'var' and 'let' keywords. Essentially they do the same thing (create a variable), except the scope of the created variable is different. Kinda neat :) Have fun coding! – Reid Pritchard Apr 09 '20 at 22:13