-1

I want to do a story problem on the following math.

A merchant sells an item at a price $210.00 and profit 5% of the price buy. Determine the purchase price the item.

The answer is like the following picture.

Then, I solved it with the code below.

  from fractions import Fraction
  def purchase_price(pp, profit):
     x = pp + profit
     return x
  pp = 100
  profit = 5
  a = pp + profit
  a = 210 * Fraction(100, 105)
  print('${:,.2f}'.format(a))

the result is like this.

TypeError: unsupported format string passed to Fraction.__format__

And I, who is still a beginner, want to ask... does my code look neat and clean?

My study materials are here and here.

Thank you, any help will be highly appreciated.

Fahrizal
  • 47
  • 6

2 Answers2

2
profit = 0.05
sell = 210
print(f"Purchase price: ${(sell/(1+profit)):.2f}")
MartinsM
  • 110
  • 7
1

Just use python's normal fraction 100/105

Your code should look like this

def purchase_price(pp, profit):
     x = pp + profit
     return x
pp = 100
profit = 5
a = pp + profit
a = 210 * (100/105)
print('${:,.2f}'.format(a))
Rodrigo Guzman
  • 487
  • 2
  • 9