3 回答
TA贡献1853条经验 获得超9个赞
此代码输出等于您的代码输出:)
sales_persons_list = [input('What is the name of the sales person?: ') for i in range(5)]
print(sales_persons_list)
sales_amounts_list = [
float(input('How much did {} make in sales?:'.format(sales_persons_list[i]))) for i in range(len(sales_persons_list))]
print(sales_amounts_list)
print("%-15s %-15s" %("Salespeople","Sales Amount"))
for i in range(len(sales_amounts_list)):
print("%-15s %-15s" %(sales_persons_list[i],sales_amounts_list[i]))
print("%-15s %-15s" %("Totals", sum(sales_amounts_list)))
将列表显示为表格代码:
print("%-15s %-15s" %("Salespeople","Sales Amount"))
for i in range(len(sales_amounts_list)):
print("%-15s %-15s"%(sales_persons_list[i],sales_amounts_list[i]))
print("%-15s %-15s" %("Totals", sum(sales_amounts_list)))
输出
Salespeople Sales Amount
a 1222.0
aa 1111.0
aaa 2222.0
aaaaa 3333.0
aaaaaa 1133.0
Totals 9021.0
TA贡献1877条经验 获得超6个赞
试试这个:
def displaySalesReport(sales_persons_list, sales_amounts_list):
Total_Sales = sum(sales_amounts_list)
print("%-15s %-15s" %("Salespeople","Sales Amount"))
for s_person, s_amount in zip(sales_persons_list, sales_amounts_list):
print("%-15s %-15s" % (s_person, s_amount))
print("%-15s %-15s" %("Totals", Total_Sales))
这是代码的简化版,您也可以使用Python Pandas创建表,Pandas最适合表格形式的数据,但它提供的功能远远超出您的需要:
import pandas as pd
data = {'Salespeople': sales_persons_list, 'Sales Amount': sales_amounts_list}
df = pd.DataFrame.from_dict(data)
df.append({'Sales Amount': Total}, index="Total")
print(df)
我的代码是伪的,您必须对其进行修改以适合您的需求。
TA贡献1906条经验 获得超10个赞
尝试使用许多流行的软件包之一来打印表格o / p。我的建议是使用tabulate
from tabulate import tabulate
sales_persons_list = list('ABCDE')
sales_amounts_list = list('12345')
print (tabulate(zip(sales_persons_list, sales_amounts_list), tablefmt="plain"))
输出
A 1
B 2
C 3
D 4
E 5
添加回答
举报