I’m trying to create a function with two inputs (number, people) whereas it searches from a list with multiple organized elements for the correct number and outputs the correct information (which the output is named: user).
Here’s the list:
JavaScript
x
2
1
List = ([15209 Amanda Craig NC], [16428 Johnathan Smith OH], [12690 Samantha Owens IL], [17002 Mark Stenson NY], [13379 Francis Trent NC], [14904 Jake Peterson CA])
2
The number request (not a user-input):
JavaScript
1
4
1
14904
2
17002
3
19999
4
Desired output:
JavaScript
1
4
1
14904 Jake Peterson CA
2
17002 Mark Stenson NY
3
"User not found"
4
This is what I have at the moment. I’m not sure if I should use split, but then I have no clue what to do next.
JavaScript
1
11
11
1
people = ([15209 Amanda Craig NC], [16428 Johnathan Smith OH], [12690 Samantha Owens IL], [17002 Mark Stenson NY], [13379 Francis Trent NC], [14904 Jake Peterson CA])
2
3
user = people[i]
4
5
def search_user(number, people):
6
ID = people.split()[0]
7
if number != ID: #I'm not sure how I can find the numbers
8
return "User not found"
9
else:
10
return user
11
If anything is wrong, it is okay to scrap it. I’m a bit confused about where to even start.
Advertisement
Answer
And if you insist on using a Tuple of lists, you should fix the syntax as was advised, which could lead for example to:
JavaScript
1
13
13
1
users = ([15209, "Amanda Craig NC"], [16428, "Johnathan Smith OH"], [12690, "Samantha Owens IL"], [17002, "Mark Stenson NY"], [13379, "Francis Trent NC"], [14904, "Jake Peterson CA"])
2
3
def search_user(number, users):
4
for user in users:
5
if user[0] == number:
6
return user
7
return "User not found"
8
9
10
print(search_user(14904, users))
11
print(search_user(17002, users))
12
print(search_user(19999, users))
13
That said, a dictionary would be far more relevant to your needs, as per the previous answer.