Reputation: 9
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.
Upvotes: 0
Views: 1062
Reputation: 95
def find_appointment(lst, title = ""):
for i in lst:
if title in i:
a=title.find(title)
return a
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"))
#hope this helps. in order to iterate through all elements in list. we have to
use a loop. once the value is found we can get its index by find method of
string.
Upvotes: 0
Reputation: 36
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"))
Upvotes: 1
Reputation: 11
What you need to use is a dictionary. So like:
appointments = {"Zoo":"11.03.22", "Shopping": "13.08.22"}
something_to_find = "Zoo"
Then to find something in the dictionary:
def find_something(lst, title=" "):
print(lst[title])
find_something(appointments,something_to_find)
So yea you might want to read up on dictionaries. W3schools is a good place to start [https://www.w3schools.com/python/python_dictionaries.asp]
Upvotes: 0