I'm finding my footing in data science and I came across an issue, I am creating a function that performs some computation for a hypothetical mobile subscription company
I created a dictionary as seen below:
mobile_sub = {'minutes_used': 200,
'minutes_included': 200,
'texts_used': 200,
'texts_included': 200,
'monthly_plan': 10,
'cost_per_minute': 0.03,
'cost_per_sms': 0.001,
'vat_tax': 0.14}
then the function for the computation :
def evaluateCharges(input_data=[]):
minutes_used = input_data['minutes_used']
texts_used = input_data['texts_used']
# Performing different actions based on the conditions indicated in the question.
if minutes_used > 200:
aditional_minutes = minutes_used-minutes_included
basecharge = monthly_plan (aditional_minutes * 0.03)
if texts_used > 200 :
aditional_texts = texts_used - texts_included
basecharge = monthly_plan (aditional_texts * 0.001)
VAT_TAX = basecharge * vat_tax
basecharge = VAT_TAX
# We can choose to format our basecharg 2dp
finalCharges = round(basecharge, 2)
# We then create a string with
output_string = "Dear Customer, below are the charges on your account : \n Minutes Used: ' str(minutes_used) '\n Texts Used: ' str(texts_used) '\n Additional Minutes: ' str(aditional_minutes) '\n Additional Texts: ' str(aditional_texts) '\n Basecharge: $' str(basecharge) '\n Tax Applied: 14% \n Total Charges: $' str(taxApplied)"
# the returning our output string
return print(output_string)
Then calling our function and passing our dictionary
evaluateCharges(input_data=[ 250, 185])
This is where I got the error :TypeError: list indices must be integers or slices, not str, how can I avoid it?
TypeError Traceback (most recent call last)
<ipython-input-25-a618d565246b> in <module>()
36 # Then calling our function and passing our dictionary
37
---> 38 evaluateCharges(input_data=[ 250, 185])
<ipython-input-25-a618d565246b> in evaluateCharges(input_data)
7 based on the values received.
8 '''
----> 9 minutes_used = input_data['minutes_used']
10 texts_used = input_data['texts_used']
11
TypeError: list indices must be integers or slices, not str
CodePudding user response:
Input data is supposed to be a map, but you are passing in [250, 185]. Then, when you index it with minutes_used = input_data['minutes_used']
, it's telling you that you can't index it with a string.
CodePudding user response:
You are passing a two element list to the evaluateCharges function which is why the error is occurring. In python, you access list indices with the syntax mylist[index]
where index is an integer. Here, you are trying to pass the strings minutes_used
and texts_used
as indices to the list called input_data
. I think you intend input_data
to be a dictionary instead of a list. It looks like you can get the desired behavior from passing your dictionary in instead:
mobile_sub = {'minutes_used': 200, 'minutes_included': 200, 'texts_used': 200, 'texts_included' : 200, 'monthly_plan': 10, 'cost_per_minute': 0.03 , 'cost_per_sms': 0.001, 'vat_tax': 0.14 }
evaluateCharges(input_data=mobile_sub)
Following this, your default value for input_data
should be a dictionary instead of a list.