Trying to find max value from a list of values generated from a loop

Viewed 32

I am really new to python and have been working on this with the help of others. I am trying to figure out the right way to go about finding the max of the list acct_all_total. Everything else works if I remove the part w/ the max function. erros i get: TypeError: 'int' object is not iterable. Or I get 0 as a result.

    def inventory(info, months_subscribed, add_free_months, video_on_demand):
        acct_all_total = 0
        acct_max = 0
         # here you init the total value to 0
        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_max = (max(acct_all_total)) #if I use these hashtag parts I get the type error mentioned above.
        
        # if int(acct_all_total) >  int(acct_all_total):
        #     acct_max = int(acct_all_total)
        #     print(f" User {info.get('name')} max value is: {acct_max} ")
    
    
    
        
        return acct_all_total ; acct_max
        
    
    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}]
    
    acct_max = 0
    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
    acct_all_total = 0
    for acct in acct_info:
        print("--")
        acct_all_total+=inventory(acct, months_subscribed, ad_free_months, video_on_demand)
    for acct in acct_info: #when I try it this way I get 0 as a result
  if int(acct_all_total) >  int(acct_all_total):
    acct_max = acct_all_total
         
    print("Total for all accounts:",acct_all_total)

Example of Output:

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 
Total for all accounts: 212
0
1 Answers

To get the biggest number in a list you can sort it first and then get the first or last item in the list.

obj = [1, 6, 9, 123, 64, 12]
obj.sort()        # Sorts the list into this: [1, 6, 9, 12, 64, 123]
print(obj[-1])    # [-1] Returns the last item in the list

Make sure the list does not contain strings as the sort function will not sort strings mixed with integers or floats.

Note that your "acct_all_total" is not a list, a list is defined with [] (insert a reference to docs or other basic explanation).

Edit: I would like to clarify that you're probably better off researching a bit more about the different types of objects there are in python, how to use them and when to use which one.

Related