Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User functions don't work:
#8
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:
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.
Reply


Messages In This Thread
User functions don't work: - by Baldev10 - Aug-17-2020, 03:47 PM
RE: User functions don't work: - by ndc85430 - Aug-17-2020, 03:50 PM
RE: User functions don't work: - by Baldev10 - Aug-17-2020, 04:02 PM
RE: User functions don't work: - by ndc85430 - Aug-17-2020, 05:02 PM
RE: User functions don't work: - by Baldev10 - Aug-17-2020, 05:50 PM
RE: User functions don't work: - by deanhystad - Aug-17-2020, 09:15 PM
RE: User functions don't work: - by Baldev10 - Aug-18-2020, 04:22 PM
RE: User functions don't work: - by deanhystad - Aug-18-2020, 08:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,149 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,568 Aug-25-2020, 03:43 AM
Last Post: micseydel
  User defined functions inside other user defined functions WildP 1 1,970 Jan-29-2020, 04:57 PM
Last Post: Clunk_Head
  Parenthesis in User-Defined Functions giorgitsu 2 2,015 Aug-07-2019, 12:56 PM
Last Post: ThomasL
  Can't work out parameter format for LibVLC functions Domarius 10 6,025 Apr-30-2019, 10:54 PM
Last Post: Domarius
  Why won't this user created function work? Evyeniarocks 5 3,160 Mar-29-2018, 05:20 PM
Last Post: wavic
  I writte 5 functions (def) but on 6 don't work. Someone can help me? perrud 2 2,824 Feb-03-2018, 10:20 AM
Last Post: perrud

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020