Northlander
Northlander

Reputation: 1

Python - Incremental variables in loop for later recall

First time working with Python and my usual "coding" is done in excel, so I'm probably doing a lot wrong. But one thing at a time. Working on a program to let users enter part number, quantity, cost, sell price, and labour hours. I currently have it spiting out profit, margin, sell price at 20% margin, and final sell price.

What I want it to be able to do is collect all the data in one loop, and print out all the data in another loop. Currently it collects and prints in the same loop because I keep writing over the variables. The goal is for every input loop to increase the counter, and then make an output loop that decreases the counter until I get to 0.

I've been unsuccessful at making a variable by combining the variable name and a counter. I had hoped to do that to have partNum1, partNum2, etc instead of just writing over partNum each time. partNum + inputCount or and variation of it using str, float, and int has not worked for me so far.

Here's the code, some of the commenting is reminders, some is simply code that didn't work but indicates the "logic" I've been trying.

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

Upvotes: 0

Views: 171

Answers (1)

itprorh66
itprorh66

Reputation: 3288

What you want is a little confusing, so bear with me as I interpret what you want to do:

  1. Read in data about part number, quantity, cost, sell price, and labour hours
  2. Given the input data compute item count, totalCost, totalPrice, totalProfit, & totalMargin
  3. Print out a list of the items entered by the user followed by the totals

Given this set of directives I would proceed as follows:

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

Upvotes: 1

Related Questions