Python新手求助:如何正确获取账户总金额列表的最大值
问题:获取账户总金额最大值时出错
我是Python新手,在他人协助下编写了账户金额计算代码,现需获取每个账户总金额的最大值。移除max函数相关代码后其余功能正常,但使用max时会触发TypeError: 'int' object is not iterable错误,另一种尝试得到的结果是0。
原代码
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.get('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)
原输出示例
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
问题分析
- max函数报错原因:
acct_all_total是单个整数,不是可迭代的列表,max()函数需要传入列表、元组这类可迭代对象,直接传整数必然触发TypeError。 - 订阅金额计算错误:第三个分支
months_subscribed_total = info.get('months_subscribed') - 3 * 7 + 18运算顺序错误,应该是(info.get('months_subscribed') - 3)*7 + 18,否则会先算3*7,导致结果错误。 - 函数返回值无效:
return acct_all_total ; acct_max在Python中只会返回第一个值acct_all_total,分号无法实现多返回值,正确写法是返回元组return acct_all_total, acct_max。 - 最大值比较逻辑错误:
if int(acct_all_total) > int(acct_all_total)是自己和自己比,永远为False,所以acct_max一直是初始值0。
修正后的代码
def inventory(info): # 初始化各项金额 months_subscribed_total = 0 ad_free_total = 0 video_on_demand_total = 0 # 计算订阅金额,修正运算顺序 months = info.get('months_subscribed') if months == 3: months_subscribed_total = months * 18 elif months < 3: months_subscribed_total = months * 7 elif months > 3: months_subscribed_total = (months - 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) print(f"Total for {info.get('name')} is: {acct_all_total} ") return 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 account_totals = [] # 收集每个账户的总金额 for acct in acct_info: print("--") total = inventory(acct) combined_total += total account_totals.append(total) # 获取最大值 acct_max = max(account_totals) print("Total for all accounts:", combined_total) print("Maximum account total:", acct_max)
修正后输出示例
-- 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 Maximum account total: 99
内容的提问来源于stack exchange,提问作者Nadia
相关产品推荐
相关产品推荐

