Advertisement
simonedare_

Discount algorithm

Nov 22nd, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. DISCOUNTS = [0.25, 0.30]
  2. THRESHOLDS = [100, 200]
  3.  
  4. # Questa funzione accetta in input una variabile numerica e
  5. # ne ritorna lo sconto.
  6. # Tipo di variable supportati:
  7. # - int (es: 4)
  8. # - float (es: 5.34)
  9. # - string (es: "100000")
  10. def get_discounted_price(price):
  11.     # if int or float
  12.     if type(price) is int or type(price) is float:
  13.         # se <= 99
  14.         if price < THRESHOLDS[0]:
  15.             print("No discount for you")
  16.             return price
  17.         # 100 < prezzo < 199
  18.         elif price >= THRESHOLDS[0] and price < THRESHOLDS[1]:
  19.             final_price = price - price * DISCOUNTS[0]
  20.             return final_price
  21.         # prezzo > 199
  22.         else:
  23.             final_price = price - price * DISCOUNTS[1]
  24.             return final_price
  25.     else:
  26.         # if string, try to cast it
  27.         try:
  28.             casted_price = float(price)
  29.             final_price = get_discounted_price(casted_price)
  30.             return final_price
  31.         except:
  32.             print("Only numbers are supported")
  33.        
  34. initial_price = "11000"
  35. final_price = get_discounted_price(initial_price)
  36. print(str(final_price) + "$")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement