I am new to python and have been trying to add values that I get from iterating over a list of dictionaries.
I keep running into 'builtin_function_or_method' object is not iterable' error message or unsupported type. Any help would be much appreciated.
here is my code:
def inventory(acct_info, months_subscribed, add_free_months, video_on_demand):
print(acct_info)
for info in acct_info:
print('-')
if info.get('months_subscribed') == 3:
months_subscribed_total = info.get('months_subscribed') * 18
elif info.get('months_subscribed') < 3:
months_subscribed_total = info['months_subscribed'] * 7
elif info.get('months_subscribed') > 3:
months_subscribed_total = info['months_subscribed'] - 3 * 7 + 18
print(f"User {info.get('name')} has months subscribed total of : $ {months_subscribed_total} ")
if info['ad_free_months'] > 0:
ad_free_total = info.get('ad_free_months') * 2
print(f" User {info.get('name')} total ad free is : {ad_free_total} ")
if info['video_on_demand'] > 0:
video_on_demand_total = info.get('video_on_demand') * 27.99
print(f" User {info.get('name')} total video on demand is : {video_on_demand_total} ")
acct_all_total = int(months_subscribed_total + ad_free_total + video_on_demand_total)
acct_all_total = [int(acct_all_total)]
print(f"Total for {info.get('name')} is: {acct_all_total} ")
acct_info = [{'name': 'acct_1', 'months_subscribed' : 2 , 'ad_free_months' : 3 , 'video_on_demand' : 1} ,
{'name': 'acct_2', 'months_subscribed' : 1 , 'ad_free_months' : 2 , 'video_on_demand' : 2},
{'name': 'acct_3', 'months_subscribed' : 2 , 'ad_free_months' : 1 , 'video_on_demand' : 3}]
combined_total = 0
months_subscribed = 0
ad_free_months = 0
video_on_demand = 0
months_subscribed_total = 0
ad_free_total = 0
video_on_demand_total = 0
inventory(acct_info, months_subscribed, ad_free_months, video_on_demand)
acct_all_total = 0
main()
Output so far is :
User acct_1 has months subscribed total of : $ 14
User acct_1 total ad free is : 6
User acct_1 total video on demand is : 27.99
Total for acct_1 is: [47]
-
User acct_2 has months subscribed total of : $ 7
User acct_2 total ad free is : 4
User acct_2 total video on demand is : 55.98
Total for acct_2 is: [66]
-
User acct_3 has months subscribed total of : $ 14
User acct_3 total ad free is : 2
User acct_3 total video on demand is : 83.97
Total for acct_3 is: [99]
What i am trying to sum up is the total for all of the users. I manage to get a total for each user, but i then want add the totals of that. Thank you.