-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.1.6.py
More file actions
62 lines (56 loc) · 1.85 KB
/
Copy path5.1.6.py
File metadata and controls
62 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#In many areas, different goods are taxed at different rates.
#Areas may charge higher tax rates for items like alcohol,
#gasoline, and soda, and lower tax rates for items like
#grocery items, medicines, and clothes.
#
#Write a class called PurchasedGood. The constructor for
#PurchasedGood should have one positional parameter called
#price, which is the price of the good as a float. It should
#then have two keyword parameters in this order:
# - category, which is the category the good falls into.
# category should have a default value of "General".
# - tax, which is the sales tax rate. tax should have a
# default value of 0.07.
#
#These three values should be stored in attributes called
#'price', 'category', and 'tax'.
#
#Then, add a method called calculate_total. calculate_total
#should calculate the price plus the price times the tax
#rate, then round the result to 2 decimal places and return
#the result. Remember, you can round to two decimal places
#using round(total, 2).
#Add your class here!
class PurchasedGood:
def __init__(self, price, category = "General", tax = 0.07):
self.price = price
self.category = category
self.tax = tax
def calculate_total(self):
total = self.price + (self.price * self.tax)
total = round(total, 2)
return(total)
#Below are some lines of code that will test your object.
#You can change these lines to test your code in different
#ways.
#
#If your code works correctly, this will originally run
#error-free and print ignoring rounding errors):
#5.0
#General
#0.07
#5.35
#5.0
#Grocery
#0.03
#5.15
good_1 = PurchasedGood(5.00)
print(good_1.price)
print(good_1.category)
print(good_1.tax)
print(good_1.calculate_total())
good_2 = PurchasedGood(5.00, category = "Grocery", tax = 0.03)
print(good_2.price)
print(good_2.category)
print(good_2.tax)
print(good_2.calculate_total())