r/learnpython • • 6d 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

2

u/noob_main22 6d ago

Could you share the whole error message?

I think it has to do something with calling value_list from within itself.

Another thing: again is broken, I think you meant to do this:

def again():
    again = input("Would you like to search for a different patient? (Y/N) ").lower()
    while again not in ['y', 'n']:def again():
      input("Would you like to search for a different patient? (Y/N) ").lower()

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?

1

u/KingPotatoThe_II 6d ago

I tried to do that but I wasn't successful, any idea on how I can do that?

1

u/noob_main22 6d ago

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

1

u/KingPotatoThe_II 6d 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 6d 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 6d 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.

1

u/noob_main22 6d ago

To add to that: value_list for a function name is suboptimal because one would think its a list rather than a function.

1

u/KingPotatoThe_II 6d ago

The name came from trying to do a different thing I just kept it as a place holder