+ 1

Help me write Python code for giving discount to product A, B, C with different price per unit

For example, product A is 30 dollars per unit. If one buys between 1 to 99 of product A, no discount. If from 100 to 199, 3% discount etc. For product B, product is $50 per unit with same discount as above. Product C is $100 per unit with same discount as above. Print the final account paying with discount given

7th Dec 2020, 12:27 AM
MUHAMMED TAHIR MUHAMMED
MUHAMMED TAHIR MUHAMMED  - avatar
13 odpowiedzi
+ 4
Code by: MUHAMMED TAHIR MUHAMMED x = "DISCOUNT CALCULATION" var1 = 4 print("MY {} ASSIGNMENT FOR QUESTION {}".format(x,var1)) product_code = str(input("Enter Product code:")) number_unit = int(input("Enter the number of unit:")) print("You have ordered for product {}, with price {}".format(product_code,number_unit)) #Product code / Set price of product if product_code == "A": price = 30 elif product_code == "B": price = 50 elif product_code == "C": price = 100 #Set discounts if 1 <= number_unit < 100: discount = 0 elif 100 <= number_unit < 500: discount = 0.01 elif number_unit > 500: discount = 0.25 else: discount = 0.2 #Create function def total_price(cost, disc, num): disc_price = cost - (cost*disc) final_price = disc_price * num return final_price #Call the function print(total_price(price, discount, number_unit))
7th Dec 2020, 4:46 AM
noteve
noteve - avatar
+ 4
Hye MUHAMMED TAHIR MUHAMMED Can you please share your code attempt? Btw 《 Nicko12 》 how do you know that he is talking about that code only? One more thing it would be pleasure if you can put the code in "a code"
7th Dec 2020, 6:07 AM
Piyush
Piyush - avatar
+ 2
First show your attempt here
7th Dec 2020, 12:29 AM
Sâñtôsh
Sâñtôsh - avatar
+ 2
I thought of using nested if-else statements but you can shorten it like using def function etc. if you want cleaner code. if (product_code=="A"): if 1<=number_unit <100: discount = 0 elif 100 <= number_unit < 500: discount = 0.01 elif number_unit > 500: discount = 0.25 else: discount = 0.2 amount_discounted = discount * 30 price=number_unit*30 final_price = price - amount_discounted elif (product_code=="B"): if 1<=number_unit <100: discount = 0 elif 100 <= number_unit < 500: discount = 0.01 elif number_unit > 500: discount = 0.25 else: discount = 0.2 amount_discounted = discount * 50 price=number_unit*50 final_price = price - amount_discounted elif (product_code=="C"): if 1<=number_unit <100: discount = 0 elif 100 <= number_unit < 500: discount = 0.01 elif number_unit > 500: discount = 0.25 else: discount = 0.2 amount_discounted = discount * 100 price=number_unit*100 final_price = price - amount_discounted
7th Dec 2020, 1:17 AM
noteve
noteve - avatar
+ 1
I was asking Muhammed Tahir bro, he is the thread starter ...
7th Dec 2020, 3:05 AM
Ipang
+ 1
Ok so I noticed something about this line. So example a costumer buys Ten $100 product with 30% or 0.30 discount amount_discounted = discount * 100 #This is equal to $30 price=number_unit*100 #10 * 100 = $1000 final_price = price - amount_discounted # $1000 - 30$ = $970 This should be like this: price = 100 discounted_price = price - (price*discount) # $100 - ($100*0.30) = $70 final_price = discounted_price *number_unit # $70 * 10 = $700 The second one is correct becuase I get the discounted price of EACH product first, notice how different they are. If you're still confused. Feel free to ask.
7th Dec 2020, 3:07 AM
noteve
noteve - avatar
+ 1
Thanks so much for your efforts and contribution
7th Dec 2020, 5:23 AM
MUHAMMED TAHIR MUHAMMED
MUHAMMED TAHIR MUHAMMED  - avatar
+ 1
Ipang I'm really Sorry I did not notice your username.
7th Dec 2020, 5:23 AM
noteve
noteve - avatar
+ 1
No problem. I remain greatful
7th Dec 2020, 5:24 AM
MUHAMMED TAHIR MUHAMMED
MUHAMMED TAHIR MUHAMMED  - avatar
0
x="DISCOUNT CALCULATION" var1=4 print("MY {} ASSIGNMENT FOR QUESTION {}".format(x,var1)) product_code=str(input("Enter Product code:")) number_unit=int(input("Enter the number of unit:")) print("You have ordered for product {}, with price {}".format(product_code,number_unit)) if (product_code=="A"): if 1<=number_unit <100: discount = 0 elif 100 <= number_unit < 500: discount = 0.01 elif number_unit > 500: discount = 0.25 else: discount = 0.2 amount_discounted = discount * 30 price=number_unit*30 final_price = price - amount_discounted
7th Dec 2020, 12:31 AM
MUHAMMED TAHIR MUHAMMED
MUHAMMED TAHIR MUHAMMED  - avatar
0
How are you calculating the discount? do you reduce from product price or from subtotal?
7th Dec 2020, 3:01 AM
Ipang
0
Help with those code for product A B and C
7th Dec 2020, 4:35 AM
MUHAMMED TAHIR MUHAMMED
MUHAMMED TAHIR MUHAMMED  - avatar
0
Piyush[21 Dec❤️] Oh, you're right, I did not save and send the final code, it would be much easier for both of us. I'll do it next time. And I don't think he is talking about the code only that's why I also explained how discount of multiple product works. Anyway, Thanks!
7th Dec 2020, 6:11 AM
noteve
noteve - avatar