Reputation: 25
I'm trying to write a function that takes three parameter (one of them is a json.file, don't worry bout this one) and returns the reaction that will happen if the patient takes a specified match. But I'm having trouble telling Python to do so. I feel like my approach using loop is wrong here, as I want the parameters to search for the right fit and produce the answer of output[][2]
output=[['Tom', 'Bee', 'Hives'],
['Bean', 'Ivy', 'Bruising'],
['Tom', 'Shellfish', 'Itching']]
def reaction(json_file, patient, substance):
for allergies in output:
for allergy in allergies:
if patient == allergy[0] and substance == allergy[1]:
result = allergy[2]
else:
return None
return result
assert reaction(ALLERGIES_FILE, 'Tom', 'Bee') == 'Hives'
assert reaction(ALLERGIES_FILE, 'Tom', 'Shellfish') == 'Itching'
Upvotes: 1
Views: 80
Reputation: 16081
You can rewrite your function like this,
def reaction(patient, substance):
result = None
for allergies in output:
if patient == allergies[0] and substance == allergies[1]:
return allergies[2]
return result
When return
on the else part the function will return None
in the first iteration. And you are iterating over sublist of output
which is unnecessary.
Also, json_file
is not being used in your method.
Execution:
In [1]: reaction('Tom', 'Bee')
Out[1]: 'Hives'
In [2]: reaction('Bean', 'Ivy')
Out[2]: 'Bruising'
Upvotes: 1