2 回答
TA贡献1821条经验 获得超6个赞
在您的问题中使用列表,稍微改变结构:
weekly_sales_list = [['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],[]]
定义从用户收集数据的方法(from random import randint用于模拟):
def ask_for_input(n):
tmp = []
for i in range(7):
# user_input = input('input value for ' + weekly_sales_list[0][i])
# print('input value for ' + weekly_sales_list[0][i])
user_input = randint(0,2) # to simulate user input (from random import randint)
tmp.append(user_input)
weekly_sales_list[1].append(tmp)
循环询问用户输入:
for n in range(3):
# print('week - ' + str(n) )
ask_for_input(n)
准备用于打印的数据和表格:
tot_per_day = [sum(col) for col in zip(*weekly_sales_list[1])]
tot_per_day = tot_per_day + [sum(tot_per_day)]
headers = weekly_sales_list[0] + ['total']
row_format ="{:>12}"*9
然后打印出来:
print (row_format.format("", *headers))
for row in weekly_sales_list[1]:
print (row_format.format("", *(row + [sum(row)])))
print ("-"*12*9)
print (row_format.format("", *tot_per_day))
这是一个随机结果:
# Monday Tuesday Wednesday Thursday Friday Saturday Sunday total
# 0 1 1 0 2 0 1 5
# 1 2 0 2 0 0 1 6
# 1 2 0 0 1 0 2 6
# ------------------------------------------------------------------------------------------
# 2 5 1 2 3 0 4 17
添加回答
举报