Aug-18-2020, 08:34 PM
(This post was last modified: Aug-18-2020, 08:35 PM by deanhystad.)
This talks about for loops:
https://docs.python.org/3.8/reference/co...-statement
This talks about dictionaries.
https://docs.python.org/3/library/stdtyp...types-dict
Global variables should be uses sparingly if at all. The only candidate for a global variable in my example is the users dictionary and I still passed that as an argument to getuser(users) so that anyone looking at getuser would know that users is an argument that is passed to the function.
In your example you could use global variables and get it closer to working, but it would be ugly. User's of your code would wonder why you made pin_no a global when the only reason for it to exist is to select a user. Making dict a global wouldn't be a terrible design decision, but you need to change the name. Don't name variables after what type they are, name them after what they represent. And do not use names that are common python words. dict is a Python function that makes a dictionary:
https://docs.python.org/3.8/reference/co...-statement
This talks about dictionaries.
https://docs.python.org/3/library/stdtyp...types-dict
Global variables should be uses sparingly if at all. The only candidate for a global variable in my example is the users dictionary and I still passed that as an argument to getuser(users) so that anyone looking at getuser would know that users is an argument that is passed to the function.
In your example you could use global variables and get it closer to working, but it would be ugly. User's of your code would wonder why you made pin_no a global when the only reason for it to exist is to select a user. Making dict a global wouldn't be a terrible design decision, but you need to change the name. Don't name variables after what type they are, name them after what they represent. And do not use names that are common python words. dict is a Python function that makes a dictionary:
x = dict(name='John Smith', age=41, occupation='Engineer') print(x) dict = {'name':'Jane Doe', 'age':26, 'occupation':'Engineer'} print(dict) y = dict(name='John Doe', age=32, occupation='Engineer') print(y)
Output:{'name': 'John Smith', 'age': 41, 'occupation': 'Engineer'}
{'name': 'Jane Doe', 'age': 26, 'occupation': 'Engineer'}
Traceback (most recent call last):
File "C:\Users\djhys\Documents\python\musings\junk.py", line 5, in <module>
y = dict(name='John Doe', age=32, occupation='Engineer')
TypeError: 'dict' object is not callable
When "dict" was used as a variable name in line 3 it added "dict" to the local namespace. The next time "dict" is used, this time as a function, it finds the variable in the local namespace and tries to treat that as a function. I see this same thing happen with list and set quite often.