r/learnpython • • 12d 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/VistisenConsult 12d 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:

import os

from typing import Optional

newLine: str = os.linesep  # \n for linux, dunno for windows

def userChoice(question, *options: str) -> str:
    """
    This function asks the user to choose between a number of options.

    Parameters
    ----------
    question: str
        The question to ask the user
    options: *str
        Any number of options the user can answer
    
    Returns
    -------
    str
        The choice chosen by the viewer
    
    Raises
    ------
    ValueError
        If the user received no options
    ValueError
        If the user gave too many wrong answers
    """
    print(question)
    if not options:
        raise ValueError("""User received no options!""")
    infoSpec: str = """Please select any of the following:%s%s%s"""
    optionStr: str = str.join(', ', options)
    info = infoSpec % (newLine, optionStr, newLine,)
    warnSpec: str = """Received '%s', but expected one of: %s%s"""
    _c: int = 10
    #  The walrus assigns choice to the response from the user and
    #  while the response is not one of the options, ask again.
    while (choice := input(info)) not in options:
        print(warnSpec % (choice, optionStr, newLine))
        if not _c:
            #  Recursion escape
            break
        _c -= 1
    else:  # The 'break' stops the loop from reaching the 'else' block
        return choice
    #  If the loop breaks, the 'return' in the 'else' block is skipped
    #  meaning we hit the error:
    raise ValueError("""Too many wrongs lol""")


if __name__ == '__main__':
    question = 'do u like fun?'
    answer = userChoice(question, 'y', 'n')
    if answer == 'y':
        print("""Use Python, it's fun!""")
    elif answer == 'n':
        print("""Try javascript, then come back to Python""")
    else:
        print("""How did you even get down here?""")