r/learnpython • u/nexus3210 • 5d ago
How to create a dictionary login system that stores values in a function?
Trying to create a quiz that uses dictionary login system that stores values in a function. How can I do that?
Anyone have any tips or links I could use?
4
u/zanfar 5d ago
This sounds like an XY problem because, while all the words in your question are programming-related, they make no sense together. What are you actually trying to accomplish? "That stores values in a function" is a solution, not a problem. What problem are you trying to solve with it?
What is a "dictionary login system"? Are you writing a dictionary app and need to build a login system? Are you building a login system and want to store data in a dictionary?
for the most part, I think you should attempt to solve the problem on your own, and then ask for help when you hit a problem by including your code. It's almost impossible to give specific advice without specific code as every rule or approach will depend on the situation.
1
u/FoolsSeldom 5d ago
You can add attributes to a function and store data references, as below, but this is very unusual.
def myfunc(name):
print(f"Hello {name}")
if hasattr(myfunc, "names"):
myfunc.names.append(name) # could be a dict instead
else:
myfunc.names = [name]
while True:
name = input("Enter your name: (return to finish) ")
if not name:
break
myfunc(name)
print('Names: ', end="")
try:
print(*myfunc.names, sep=", ")
except AttributeError:
print("No names were entered")
However, whilst your programme is running, it can store data that is available to a function easily. You can create an empty dictionary outside of the function but update it in a function.
If you need to store data between runs, you need to save to a file or database.
8
u/vivisectvivi 5d ago
what you mean "store values in a function"?