'Calculate The Discount of a Product using a Python Code
I am currently trying to create a code that will calculate the discount (in percentage) of a product.
The code is supposed to ask the user of the code for the original price of the code, the discounted price, then it should tell the user what the discount was (in percentage). But it is not showing the correct percentage right now.
Here is the code so far:
o = float(input('The original ticket price: $'))
d = float(input('The price after the discount: $'))
p = 100/(o/d)
p2 = str(round(p, 2))
print('The discount (in percentage) is', p2,'%')
Solution 1:[1]
The formula you are using is not correct, try this:
o = float(input('The original ticket price: $'))
d = float(input('The price after the discount: $'))
p = (1 - d / o) * 100
p2 = str(round(p, 2))
print('The discount (in percentage) is', p2,'%')
Solution 2:[2]
The calculations that you put in your code are incorrect. Here's an improved version that I whipped up, and I hope this helps you:
if option == 3:
o = float(input('The ticket (original) price($): '))
dp = float(input('Price after the discount($): '))
p = 100/(o/dp)
d = str(round(100 - p, 2))
print('The percentage discount is', d,'%')
Solution 3:[3]
Just did something similar to this myself. Here's the code I used:
It's essentially using the following formula to work out the amount saved, then deducts it from the main cost:
Total = cost * sunflower
Discount = Total * 0.1
FinalCost = Total - Discount
An actual functional piece of code would look like:
# $5.00 Sunflowers at 10% off calculator:
#variables
cost = 5
Sunflowers = 10
#calculator
Total = float(cost) * int(Sunflowers)
Discount = float(Total) * 0.1
FinalCost = float(Total) - float(Discount)
print("Final price of: ", Sunflowers , ": $", FinalCost)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jailton Silva |
Solution 2 | |
Solution 3 | SunflowerTwix |