【发布时间】:2021-04-20 01:49:34
【问题描述】:
此函数tickets_ages() 计算每个年龄组购买的活动门票数量。
我需要创建一个可视化表示来显示每个年龄段的门票销售情况,但我不知道该怎么做。我已经完成了基本功能,该函数只输出每个年龄段+售出的门票数量。
计算:函数取最大数,在本例中为 18,然后除以 4,即 4.5。 4.5 将代表图表上的一个刻度线*
对于其余数字,每个数字除以 4.5,与最大除法因子 18 相同。
0 to 20: 12 ---> becomes 2.66
20 to 40: 15 ---> becomes 3.33
40 to 60: 16 ---> becomes 3.55
60 to 80: 15 ---> becomes 3.33
80 and over: 18 ---> 4.5
这是我的代码:
def sort_list(value):
sorted_v = list(set(value)) #This just puts the values once so they dont repeat, dont worry about it
sorted_v.sort()
return sorted_v
'''
The list
'''
ages = ['0 to 20', '0 to 20', '0 to 20', '0 to 20','0 to 20', '0 to 20', '0 to 20', '0 to 20','0 to 20', '0 to 20', '0 to 20', '0 to 20', '20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80', '40 to 60','60 to 80','60 to 80', '80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','40 to 60','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80', '40 to 60','60 to 80','80 and over']
'''
Main function
'''
def tickets_ages(ages):
sort_thelist = sort_list(ages)
string_s = ''
for source in sort_thelist:
output = ages.count(source)
string_s += '{}: {} \n'.format(source, output)
return string_s
'''
Test code
'''
print(tickets_ages(ages))
输出应该是:
0 to 20: *** #---> 2.66
20 to 40: *** #---> 3.33
40 to 60: *** #---> 3.55
60 to 80: *** #---> 3.33
80 and over: ***** #---> 4.5
【问题讨论】:
-
提示:
'*' * 10→'**********' -
@martineau 是的,我知道,我的主要问题是我不知道如何专门针对每个特定年龄组执行此操作,并获得最大数量的年龄组,将其除以 4 和剩下的 4.5,如果你能给我一个关于如何做的提示,那就太好了!
-
我是在暗示要回答名义上的问题——如何制作图表。要做其他事情,我建议将每个年龄段的数字存储在某种类型的容器中,比如列表或字典.然后,您可以轻松地在评论中完成您现在询问的所有其他事情。 Python 的内置容器数据结构是它的主要优势之一——尝试更好地利用它们。
标签: python python-3.x