循环中的Python增量变量供以后调用

2024-05-20 14:17:21 发布

您现在位置: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")

Tags: ofinputfloatintprintpartstrlabour
1条回答
网友
1楼 · 发布于 2024-05-20 14:17:21

你想要的有点让人困惑,所以请耐心听我解释你想要做的事情:

  1. 读入有关零件号、数量、成本、售价和工时的数据
  2. 给定输入数据,计算项目计数、总成本、总价格、总利润&;总利润
  3. 打印出用户输入的项目列表,然后是总计

鉴于这一系列指示,我将按照以下步骤进行:

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}")

相关问题 更多 >