python - Python - 循环中的增量变量供以后调用
问题描述
第一次使用 Python,我通常的“编码”是在 excel 中完成的,所以我可能做错了很多。但一次只做一件事。开发一个程序,让用户输入零件编号、数量、成本、售价和工时。我目前有它吐出利润、保证金、20% 保证金的售价和最终售价。
我希望它能够在一个循环中收集所有数据,并在另一个循环中打印出所有数据。目前它在同一个循环中收集和打印,因为我一直在写变量。目标是让每个输入循环都增加计数器,然后创建一个输出循环来减少计数器,直到我达到 0。
通过组合变量名和计数器来创建变量,我一直没有成功。我曾希望这样做有 partNum1、partNum2 等,而不是每次都写 partNum。到目前为止,partNum + inputCount 或它的变体使用 str、float 和 int 对我不起作用。
这是代码,一些评论是提醒,一些只是不起作用但表明我一直在尝试的“逻辑”的代码。
#program to help with quoting and calculating margin
#declare a few global variables before the program gets angry about calling them
inputCount = 0
totalCost = 0
totalPrice = 0
totalProfit = 0
totalMargin = 0
moreParts = 'y'
#input section
while moreParts != 'n':
#while moreParts != 'n' or 'N' or 'No' or 'no' or 'NO':
#increase counter
inputCount += 1
#inputCount = inputCount + 1
#get part number
print ('Part number: ')
partNum = input()
#partNum + inputCount = str(currentPart)
#str(partNum) + str(inputCount) = input()
#get part quantity
print ('Quantity: ')
partQty = input()
#get part cost
print ('Cost: ')
partCost = input()
partCost = float(partCost) * int(partQty)
#get part sale price
print ('Sale price: ')
partPrice = input()
partPrice = float(partPrice) * int(partQty)
#calculate profit & margin
partProfit = float(partPrice) - float(partCost)
partMargin = float(partProfit) / float(partPrice) * 100
#increase totals for each part entered
totalCost += float(partCost)
totalPrice += float(partPrice)
#totalCost = float(totalCost) + float(partCost)
#totalPrice = float(totalPrice) + float(partPrice)
totalProfit = float(totalPrice) - float(totalCost)
totalMargin = float(totalProfit) / float(totalPrice) * 100
twentyPoints = float(totalCost) * 1.25
#Summary of the data entered
print ()
print ('* * * Report Section * * *')
print ('PN: ' + str(partNum))
print ('Qty: ' + str(partQty))
print ('Cost: ' + str(partCost))
print ('Sale Price: ' + str(partPrice))
print ('Profit: ' + str(partProfit))
print ('Margin: ' + str(partMargin) + '%')
print ()
print ('Add another part? (y/n) ')
moreParts = input(str())
#end of while loop
#calculating labour section
print ()
print ('Hours of labour: ')
hours = input()
#uncomment these lines if changing shop rate from $145/hour
#print ('Hourly labour rate: ')
#rate = input()
rate = 145
labour = float(hours) * float(rate)
totalFinal = float(labour) + float(totalPrice)
#Final summary of report
print ()
print (' * * * SUMMARY * * *')
print ('Different part numbers: ' + str(inputCount))
#while inputCount <= 0:
#print ((str(partQty) + int(inputCount)) + ' of ' (str(partNum) + int(inputCount))
#int(inputCount) - 1
print ('Total cost of parts: $' + str(totalCost))
print ('Profit: $' + str(totalProfit))
print ('Margin: ' + str(totalMargin) + '%')
print ('Sale price of parts: $' + str(totalPrice))
print ('Sale price at 20% margin: $' +str(twentyPoints))
print ('Total labour charge: ' + str(labour))
print ()
print ('Before tax parts & labour price: $' +str(totalFinal))
print ()
input("Press enter to exit")
解决方案
你想要的有点令人困惑,所以请耐心等待我解释你想要做什么:
- 读入有关零件编号、数量、成本、售价和工时的数据
- 给定输入数据计算项目计数、totalCost、totalPrice、totalProfit 和 totalMargin
- 打印出用户输入的项目列表,然后是总数
鉴于这组指令,我将按如下方式进行:
def get_order():
# Process the inputs and create the order_list
order_list = []
# Gather the Input data
while True:
order = {'Part Number': None, 'Quantity': None, 'Part Cost': None, 'Part Price': None}
for itm in order.keys():
ntry = input(itm+": ")
if itm == "Part Number":
order[itm] = ntry
elif itm == "Quantity":
order[itm] = int(ntry)
else:
order[itm] = float(ntry)
order['Order Cost'] = order["Part Cost"] * order['Quantity']
order['Order Price'] = order['Part Price'] * order['Quantity']
order['Order Profit'] = order['Order Price'] - order['Order Cost']
order['Order Margin'] = 100*(order['Order Profit']/order['Order Price'])
order_list.append(order)
if input('Enter More Parts').lower()[0] == 'n':
break
return order_list
# Get the order list
ordr_lst = get_order()
print(ordr_lst)
# Now Prtocess the order list
totalCost = 0
totalPrice = 0
totalProfit = 0
totalMargin = 0
li = 1
print('**** Summary Report *******')
print(f'Number of Items: {len(ordr_lst)}')
for ordr in ordr_lst:
totalCost += ordr['Order Cost']
totalPrice += ordr['Order Price']
totalProfit += ordr["Order Profit"]
totalMargin += ordr['Order Margin']
print(totalCost, totalPrice, totalProfit, totalMargin)
s = f"{ordr['Quantity']} of {ordr['Part Number']} sold for ${ordr['Order Price']}"
print (s)
print(f'Total Cost of Parts ${totalCost}')
print(f"Profit: ${totalProfit}")
推荐阅读
- excel - Excel VBO - Blue Prism 中的数据透视表报表布局
- spring-boot - java.lang.ClassCastException:实体 A 与实体 B 不兼容
- php - 在开关内声明数组并设置值
- flutter - 将文本拟合/绘制到颤动的形状?
- css - 在 WordPress Gutenberg 图像块中为图像添加阴影
- python - python使用多个条件创建一个新列
- sql-server - SQL Server NEWSEQUENTIALID() - 超快速 .net 核心实现的说明
- css - 我正在尝试纠正纯CSS中三个图像的无限翻转、旋转、旋转和消失的动画持续时间和时间线
- javascript - 如何更改变量onclick
- python - 如何在 Django 中输出 FilePathField 的完整路径?