我目前正在一个项目上,我需要将列表转换为表格的帮助。我已经想出了一种方法,但是必须有一种更短,更清洁的方法来解决这个问题,而我在弄清楚这一点时遇到了麻烦。我的编码如下(我需要帮助的部分在**的底部)
def main():
# Create a list for the sales person
sales_persons_list = inputSalesPeople()
sales_amounts_list = inputSalesAmounts(sales_persons_list)
displaySalesReport(sales_persons_list, sales_amounts_list)
# Create a list for the sales person
def inputSalesPeople():
sales_persons_list = []
count = 0
while count < 5:
Name = input("What is the name of the sales person?: ")
count += 1
sales_persons_list.append(Name)
print(sales_persons_list)
return sales_persons_list
def inputSalesAmounts(sales_persons_list):
sales_amounts_list = []
index = 0
while index < len(sales_persons_list):
Sales =float(input("How much did " + str(sales_persons_list[index]) + " make in sales?: $"))
index += 1
sales_amounts_list.append(Sales)
print(sales_amounts_list)
return sales_amounts_list
**def displaySalesReport(sales_persons_list, sales_amounts_list):
Total_Sales = sum(sales_amounts_list)
print("%-15s %-15s" %("Salespeople","Sales Amount"))
print("%-15s %-15s" %(sales_persons_list[0],sales_amounts_list[0]))
print("%-15s %-15s" %(sales_persons_list[1],sales_amounts_list[1]))
print("%-15s %-15s" %(sales_persons_list[2],sales_amounts_list[2]))
print("%-15s %-15s" %(sales_persons_list[3],sales_amounts_list[3]))
print("%-15s %-15s" %(sales_persons_list[4],sales_amounts_list[4]))
print()
print("%-15s %-15s" %("Totals", Total_Sales))**
main()
这将显示具有正确值的正确表,但是如您所见,它有点长且不干净。我该如何缩短?感谢您的帮助!
暮色呼如
慕哥9229398
隔江千里
相关分类