Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I return element from a list based on user input

I’m supposed to make a function with a list and a title as a string, and then return the item from
the list, based on the title.

def find_appointment(lst, title = ""):
    if title in lst:
        funn = lst.find(title)
        return funn
    else:
        print("No result")

appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
find_appointment(appointments, "Zoo")

I hoped to get "Zoo: 11.03.22", but instead got "No result"

The list here is just a random one I made up. In the actual list I won’t know the positions of
the items.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here is my solution:

def find_appointment(lst, title = ""):
    for i in lst:
        if title in i:
            return index
        else:
            print("No result")

appointments = ["Zoo: 11.03.22", "Shopping: 13.08.22", "Christmas: 24.12.22", "Funeral: 25.12.22"]
print(find_appointment(appointments, "Zoo"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading