0
\$\begingroup\$

I need help with this program:

Create a program that will accept the user's name and ID number. The program will then ask for the department code, 1 is for the Toy Section, 2 for Ladies' Wear, 3 for Men's Wear, and 4 for Kiddie Section. If the user enters 1 (for example), it will show the products on the Toy Selection and their prices. Afterwards, it will ask the user to enter which product they want to buy then the quantity of the product. It will display the total and determine the commission. If the total is less than 5000, then there's no commission. Otherwise, there will be a 10% commission if the total is over 5000, 20% if it's over 10,000 and 30% if the total is 15,000.

Available products in Toy Section:

A - Toy Car        Price: 300     
B - Stuff Toys     Price: 500

Available products in Ladies' Section:

C - Pants        Price: 799
D - Blouse       Price: 599
E - Underwear    Price: 499

Available products in Men's Wear Section:

F - Necktie        Price: 249
G - T-shirt        Price: 349
H - Belt           Price: 199

Available products in Kiddies Section:

I - Milk Bottle        Price: 99
J - Walker             Price: 899

This is my code:

rep_name = input("Enter Sales' Representative Name: ")
id_number = input("Enter ID Number: ")
department_code = int(input("Enter Department Code: "))
match department_code:
    case 1:
        department_code = {"Toy"}
        priceA = int(300)
        priceB = int(500)
        for i in department_code:
            print("You are in",i,"Section.")
            print("Available products in",i,"Section:\nA - Toy Car        Price:",priceA,"\nB - Stuff Toys     Price:",priceB)
    case 2:
        department_code = {"Ladies' Wear"}
        priceC = int(799)
        priceD = int(599)
        priceE = int(499)
        for i in department_code:
            print("You are in",i,"Section.")
            print("Available products in",i,"Section:\nC - Pants        Price:",priceC,"\nD - Blouse       Price:",priceD,"\nE - Underwear    Price:",priceE)
    case 3:
        department_code = {"Men's Wear"}
        priceF = int(249)
        priceG = int(349)
        priceH = int(199)
        for i in department_code:
            print("You are in",i,"Section.")
            print("Available products in",i,"Section:\nF - Necktie        Price:",priceF,"\nG - T-shirt        Price:",priceG,"\nH - Belt           Price:",priceH)
    case 4:
        department_code = {"Kiddie"}
        priceI = int(99)
        priceJ = int(899)
        for i in department_code:
            print("You are in",i,"Section.")
            print("Available products in",i,"Section:\nI - Milk Bottle        Price:",priceI,"\nJ - Walker             Price:",priceJ)
    case _:
        print("Code is incorrect!")

# total amount <5000 = no commission
# total amount >=5000 = 10% commission
# total amount >= 10000 = 20% commission
# total amount >= 15000 = 30% commission
product = input("Enter product: ")
match department_code and product:
    case 1 | 'a' | 'A':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceA)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 1 | 'b' | 'B':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceB)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 2 | 'c' | 'C':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceC)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 2 | 'd' | 'D':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceD)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 2 | 'e' | 'E':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceE)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 3 | 'f' | 'F':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceF)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 3 | 'g' | 'G':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceG)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 3 | 'h' | 'H':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceH)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 4 | 'i' | 'I':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceI)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case 4 | 'j' | 'J':
        commission = float(0)
        quantity = int(input("Quantity: "))
        totalAmount = int(quantity * priceJ)
        print("Total Amount:",totalAmount)
        if (totalAmount >=5000) and (totalAmount <=9999):
            commission = totalAmount*0.1
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=10000) and (totalAmount <=14999):
            commission = totalAmount*0.2
            print("commission:", "%.2f" % commission)
        elif (totalAmount >=15000):
            commission = totalAmount*0.3
            print("commission:", "%.2f" % commission)
        else:
            print("commission:", int(commission))
    case _:
        print("Invalid input.")

This is the output of my code:

Enter Sales' Representative Name: Shane
Enter ID Number: 24
Enter Department Code: 3
You are in Men's Wear Section.
Available products in Men's Wear Section:
F - Necktie        Price: 249
G - T-shirt        Price: 349
H - Belt           Price: 199
Enter product: g
Quantity: 56
Total Amount: 19544
Commission: 5863.20

How can I improve my code?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Don't repeat yourself. This code

commission = float(0)
quantity = int(input("Quantity: "))
totalAmount = int(quantity * priceA)
print("Total Amount:",totalAmount)
if (totalAmount >=5000) and (totalAmount <=9999):
    commission = totalAmount*0.1
    print("commission:", "%.2f" % commission)
elif (totalAmount >=10000) and (totalAmount <=14999):
    commission = totalAmount*0.2
    print("commission:", "%.2f" % commission)
elif (totalAmount >=15000):
    commission = totalAmount*0.3
    print("commission:", "%.2f" % commission)
else:
    print("commission:", int(commission))

is repeated for every product you have. Imagine the store gets 1000 new products. And then you need to change the string being printed. In every single one of those 1000 blocks.

Python has a def keyword that allows you to extract code into a function that can be invoked wherever you like:

def print_commission(price):
    commission = float(0)
    quantity = int(input("Quantity: "))
    totalAmount = int(quantity * price)
    print("Total Amount:",totalAmount)
    if (totalAmount >=5000) and (totalAmount <=9999):
        commission = totalAmount*0.1
        print("commission:", "%.2f" % commission)
    elif (totalAmount >=10000) and (totalAmount <=14999):
        commission = totalAmount*0.2
        print("commission:", "%.2f" % commission)
    elif (totalAmount >=15000):
        commission = totalAmount*0.3
        print("commission:", "%.2f" % commission)
    else:
        print("commission:", int(commission))

Then invoke it with whatever price you need:

print_commission(priceA)

This applies to your first match statement too.


Storing prices separately isn't practical (imagine you have 1000 of them), use a dict instead :

products = {"Toy Car": 300, "Stuff Toys": 500, "Pants": 799}

priceF = int(249)
priceG = int(349)
priceH = int(199)

This can be simplified:

priceF = 249
priceG = 349
priceH = 199

since those numbers are already of type int.


match department_code:
    case 1:
        department_code = {"Toy"}

Don't reassign a name to an object of different type. This is confusing and breaks any logic that might depend on the old type.


As an exercise try to generate lots of different products and departments randomly and make the logic independent from their concrete instances.

\$\endgroup\$
1
\$\begingroup\$

Indeed it makes sense to create a function for calculating the commission, because there is a lot of repetition here. Besides, the rules are uniform across all departments so it's not necessary to customize rules.

So this is a quick win. Externalizing this logic will de-clutter your code. In fact it can even be kept in a separate file, that you import whenever needed and can reuse elsewhere.

I would propose an implementation along these lines, normalized to snake case as per PEP8:

def calculate_commission(quantity, price):
    coef: float = 0
    total_amount = quantity * price

    if 5000 <= total_amount <= 9999:
        coef = 0.1
    elif 10000 <= total_amount <= 14999:
        coef = 0.2
    elif total_amount >= 15000:
        coef = 0.3

    return total_amount * coef

The idea is that a function does only one thing, but does it well. So this function does not request input from the user, and does not print anything. It's up to the caller to use the return value as appropriate.

For your products, use a dict as shown by QuasiStellar or store the data in a separate JSON file, so as to better separate code from data. This is definitely more convenient if you have a lot of items. Use import json and then json.load and voilà you have a neat dict-like structure from your JSON file, that is easy to handle in Python.

Example of a list of dict with no hierarchy. This is a simple, flat structure that should be enough for your purpose:

products = [
    {"code": "A", "description": "Toy Car", "price": 300, "department": "Toy"},
    {"code": "A", "description": "Stuff Toys", "price": 500, "department": "Toy"},
    {"code": "C", "description": "Pants", "price": 799, "department": "Ladies"},
    {"code": "D", "description": "Blouse", "price": 599, "department": "Ladies"},
    {"code": "E", "description": "Underwear", "price": 499, "department": "Ladies"}
]

Then to iterate on the products:

for product in products:
    if product['department'] == "Toy":
        print(f"code: {product['code']} - description: {product['description']} - price: {product['price']} - department: {product['department']}")

You will notice that this way of doing things is more flexible. All your product data is centralized so it's easier to maintain. And the number of products in any department can be variable and won't break up your code, or require additional variables.

Also read up on list comprehensions. Sooner or later you will get to use them a lot. It would work like this:

toy_products = [item for item in products if item['department'] == "Toy"]

Essentially, what you are doing is filtering a list for convenient processing.

Again, all that data does have to be present in the same file where your code resides. It is perfectly allowed to save it in a different file, and you just import it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.