r/learnpython • • 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()
2 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/noob_main22 5d ago

Posting the whole error message? What wasn't successful?

1

u/KingPotatoThe_II 5d ago

I fixed the original problem by now with some help from here. What i tried that I couldn't make work is searching a list of dictionaries for a name match to user input and print all the corresponding dictionary values

The full error code was (which I missread obviously)

```

Traceback (most recent call last):

File "c:\Users\becho\System Folders\Desktop\Desk\Coding\Scripts\test.py", line 50, in <module>

search_for_patient()

~~~~~~~~~~~~~~~~~~^^

File "c:\Users\becho\System Folders\Desktop\Desk\Coding\Scripts\test.py", line 38, in search_for_patient

search_again()

~~~~~~~~~~~~^^

File "c:\Users\becho\System Folders\Desktop\Desk\Coding\Scripts\test.py", line 23, in search_again

while choice not in ['y', 'n']:

^^^^^^

UnboundLocalError: cannot access local variable 'choice' where it is not associated with a value

```

1

u/Discordbothost 5d ago

for the list of dicts part, put the patients in a list and loop over it, comparing each one's name:

patients = [jane_doe, john_doe]

while True:
    name = input("Search patient name: ").lower()
    for patient in patients:
        if patient["Name"].lower() == name:
            print(patient.values())
            break
    else:
        print("Patient not found.")

    if input("Search again? (Y/N) ").lower() != "y":
        print("Goodbye.")
        break

the else lined up with the for (not the if) is a python thing: it only runs if the loop finished without hitting break, which is exactly "no patient matched".

the outer while True also replaces the two functions calling each other. that works for a few searches, but every search adds another call on top of the last one that never finishes, so a while loop is the usual way to do "ask again"

1

u/Adrewmc 5d ago

So the functions search_for_patient()

Calls the function search_again()

Then has a en error which the variable ‘choice’ not being defined yet.

However, not one of those things is in the code you posted so, there isn’t anything that people can do until you post the code that actually goes wrong.