223k views
2 votes
(100 POINTSSSS!) Using Python, solve this third problem.

(100 POINTSSSS!) Using Python, solve this third problem.-example-1

1 Answer

3 votes

Answer:

from enum import Enum

class Material(Enum):

GoldPlated = 1

SolidGold = 2

Copper = 3

def calculatePrice(material: Material, units):

match material:

case Material.GoldPlated:

return 50.0+units*7

case Material.SolidGold:

return 100.0+units*10

case _:

raise ValueError('Unknown material '+material.name)

try:

print("gold plated with 12 units: ${:.2f}".format(calculatePrice(Material.GoldPlated, 12)))

print("solid gold with 10 units: ${:.2f}".format(calculatePrice(Material.SolidGold, 10)))

print("solid gold with 5 units: ${:.2f}".format(calculatePrice(Material.SolidGold, 5)))

print("copper with 3 units: ${:.2f}".format(calculatePrice(Material.Copper, 3)))

except Exception as err:

print(err)

Step-by-step explanation:

I used an enum and exception handling here to make it more interesting.

User Thomas Dickey
by
7.7k points