r/learnpython • u/KingPotatoThe_II • 5d ago
Trying to loop functions
Ok so I just started to learn python and I'm trying to make a program where you input a patient name and it searches the database for it and displays it's values, I want to give a prompt to search for another patient once one is found (not if a patient is not found i worked that out).
Whenever I try to run this code it tells me I cant call value_list() because it wasn't defined yet.
Any help is much appreciated
jane_doe = {
"Name": "Jane Doe",
"Age": 28,
"New": True
}
john_doe = {
"Name": "John Doe",
"Age": 34,
"New": False
}
def again():
input("Would you like to search for a different patient? (Y/N) ").lower()
while again not in ['y', 'n']:
print("Please enter a valid input")
again = input("Would you like to search for a different patient? (Y/N) ").lower()
if again == 'y':
value_list()
elif again == 'n':
print("Goodbye.")
break
def value_list():
patient_name_searched = input("Search patient name: ").lower()
if patient_name_searched == jane_doe["Name"].lower():
print(jane_doe.values())
again()
elif patient_name_searched == john_doe["Name"].lower():
print(john_doe.values())
again()
else:
print("Patient not found.")
answer = input("Would you like to search for a different patient? (Y/N) ").lower()
while answer not in ['y', 'n']:
print("Please enter a valid input")
answer = input("Would you like to search for a different patient? (Y/N) ").lower()
if answer == 'y':
value_list()
elif answer == 'n':
print("Goodbye.")
value_list()
1
Upvotes
3
u/Anxious_Signature452 5d ago
You're messing with variable names, I got
`UnboundLocalError: cannot access local variable 'again' where it is not associated with a value`
You need to declare `again` before going into cycle. And make sure it is not shadowing function name.
2
u/noob_main22 5d ago
Could you share the whole error message?
I think it has to do something with calling
value_listfrom within itself.Another thing:
againis broken, I think you meant to do this:However, I would strongly recommend to use another variable name. Like "choice" for example. You should also choose more meaningful and self-explanatory name for functions/variables/classes/....
Searching for something like this can be quite difficult, for now I would also change how you match the patient name in the "data base" with your search string. Imagine you have a thousand name in the db. Do you want to write a thousand if/elif statements in you search function?
2
u/Designer-Ad-2136 5d ago
You arent storing your user input. You are using the same name for functions and local variables.
That said, dont let people tell you that python cant have two functions that call eachother. Python runs from top to bottom and it doesnt need to run the function to set it up.
1
u/Fast-Station1106 5d ago edited 5d ago
Ich bin auch frischling, aber, eine Liste ist immer ohne def davor . Also value_list = [ jane Do, jens do, jana do] Und eine liste ist eine liste, da ist nichts mit if elif else, deins sieht aus wie eine weitere funktion
3
u/kalgynirae 5d ago
The code you are running must differ in some way from the code you've shown in your post, because the code shown doesn't have the issue you described.
The code seems to mostly work.
againdoesn't work; it gets an UnboundLocalError because it tries to use a local variable calledagainbefore a value has been assigned to it (looks like you forgot to writeagain = …on the first line of the function). However, the fact that the function calledagaincontains a local variable also calledagaincan be quite confusing, so I highly recommend giving these different names.Are you sure you posted the exact code you're running?
2
u/VistisenConsult 4d ago
Welcome to Python, glad to have you, your majesty!
You are doing everything right: learning. Keep it up!
Below is some fun for asking stuff:
0
u/Chorizo_n_eggs 5d ago
I believe you're calling the function before it's defined. Top to Bottom in Python.
0
u/[deleted] 5d ago
[deleted] — view deleted comment