Here’s a Python program that accomplishes both parts of your assignment. The code will take a given UPC code, split it into its components, and then calculate the total cost based on the specified quantity and unit price.
# Part 1: Decode UPC code
def decode_upc(upc_code):
# Split the UPC code into its respective parts
upc_parts = {
‘1-Digit Number’: upc_code[0],
‘Manufacturer Code’: upc_code[1:6],
‘Product Code’: upc_code[6:11],
‘Check Digit’: upc_code[11]
}
return upc_parts
# Part 2: Calculate costs
def calculate_cost(unit_cost, quantity):
total_cost = unit_cost * quantity
return total_cost
# Main program
def main():
upc_code = “020357122682”
# Decode UPC Code
upc_parts = decode_upc(upc_code)
# Print the decoded parts
print(f”1-Digit Number: {upc_parts[‘1-Digit Number’]}”)
print(f”Manufacturer Code: {upc_parts[‘Manufacturer Code’]}”)
print(f”Product Code: {upc_parts[‘Product Code’]}”)
print(f”Check Digit: {upc_parts[‘Check Digit’]}”)
# Cost calculation
unit_cost = 275.15
quantity = 12
total_cost = calculate_cost(unit_cost, quantity)
# Print out the purchase details
print(f”\nQuantity to Purchase: {quantity}, Unit Cost: ${unit_cost:,.2f}, Total Cost: ${total_cost:,.2f}”)
if __name__ == “__main__”:
main()
Explanation:
1. Part 1:
– The function decode_upc takes a UPC code as input, splits it into its respective components (1-digit number, manufacturer code, product code, and check digit), and returns a dictionary containing these parts.
– The main program prints each part of the UPC code.
2. Part 2:
– The function calculate_cost multiplies the unit cost by the quantity to get the total cost.
– The main program calculates the total cost based on a unit cost of $275.15 for 12 units and prints the quantity, unit cost (formatted to two decimal places), and total cost (also formatted with commas and two decimal places).
Output:
When you run this program, it will produce output like this:
1-Digit Number: 0
Manufacturer Code: 20357
Product Code: 12268
Check Digit: 2
Quantity to Purchase: 12, Unit Cost: $275.15, Total Cost: $3,301.80
This output clearly displays the decoded UPC code components and the financial details of the purchase. You can replace the upc_code variable with any valid UPC to test different codes.