creating a class and making instances from it, cant get this to work . | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

creating a class and making instances from it, cant get this to work .

class Car(object): def __init__(self, price, speed, fuel, mileage, price_plus_tax=value): self.price = price self.speed = speed self.fuel = fuel self.mileage = mileage self.price_plus_tax = price_plus_tax if price > 10000: self.tax = 0.15 else: self.tax = 0.12 self.displayAll() def price_plus_tax(self, price_plus_tax=value): if price > 10000: self.price_plus_tax = self.price + self.price * self.tax; else: self.price_plus_tax = self.price + self.price * self.tax; self.displayAll() def displayAll(self): print ('Price: ' + str(self.price)) print ('Speed: ' + str(self.speed) + ' mph') print ('Fuel: ' + self.fuel) print ('Mileage: ' +str(self.mileage) + ' mpg') print ('Tax: ' + str(self.tax)) print ('price_plus_tax: ' + str(self.price_plus_tax)) #six instances of Car class car1 = Car(5000, 40, 'Half Full', 10000) car2 = Car(15000, 100, 'Full', 65000) car3 = Car(2000, 50, 'Quarter Full', 300000) car4 = Car(10000, 100, 'Mostly Full', 25000) car5 = Car(17000, 75, 'Full', 40000) car6 = Car(9000, 90, 'Empty', 55000)

7th Jan 2019, 1:17 AM
Instructor M
Instructor M - avatar
15 Answers
+ 3
Diego Acero I don't get any error 🤔 I just wanted to point out that the final price isn't quite right because it will add $ 5,000.10 taxes for a $ 5,000 car ☺️
7th Jan 2019, 4:56 AM
Anna
Anna - avatar
+ 3
Instructor M Make sure that def displayAll(self) is on the same indentation level as def __init__(self) and the other class methods. There has to be the exact same amount of spaces (usually four) at the beginning of the lines.
7th Jan 2019, 7:56 PM
Anna
Anna - avatar
+ 2
1. Comment (or delete) line 2. 2. Change lines 3-21 for this: class Car(object): def __init__(self, price, speed, fuel, mileage): self.price = price self.speed = speed self.fuel = fuel self.mileage = mileage if price > 10000: self.tax = 0.15 else: self.tax = 0.10 self.price_plus_tax = self.price + self.price * self.tax self.displayAll() EDIT: Small mistake. Changed "...self.price + self.tax" to "...self.price * self.tax"
7th Jan 2019, 3:45 AM
Diego
Diego - avatar
+ 2
Change the formula to calculate the price plus tax to self.price + self.price * self.tax. A method could look like this: self.price_plus_tax = self.calculate_price_plus_tax() def calculate_price_plus_tax(self): if self.price > 10000: tax = 0.15 else: tax = 0.10 return self.price + self.price * tax
7th Jan 2019, 4:48 AM
Anna
Anna - avatar
+ 1
1. Variable "value" is not defined. 2. Function "price_plus_tax" is never used. 3. Next time please paste your code in the Code Playground and share the link.
7th Jan 2019, 2:14 AM
Diego
Diego - avatar
+ 1
Hello thanks again . I made the change . Having indentation error . File "..\Playground\", line 15 def displayAll(self): ^ IndentationError: unindent does not match any outer indentation level
7th Jan 2019, 4:33 AM
Instructor M
Instructor M - avatar
+ 1
That's weird! Tried it in the Code Playground and in other IDE and no error message showed up.
7th Jan 2019, 4:40 AM
Diego
Diego - avatar
+ 1
Anna Is that what's causing the problem? If so, could you please explain why?
7th Jan 2019, 4:56 AM
Diego
Diego - avatar
+ 1
Anna Oh, right! Missed that. Thanks.
7th Jan 2019, 4:58 AM
Diego
Diego - avatar
+ 1
Change lines 11-14 for this: self.price_plus_tax = self.calculate_price_plus_tax() self.displayAll()
8th Jan 2019, 6:40 AM
Diego
Diego - avatar
0
Thanks here is the link , how do i fx it . https://code.sololearn.com/c6KgyN2fw0U8
7th Jan 2019, 3:35 AM
Instructor M
Instructor M - avatar
0
thanks . I would like to add either an attribute showing final price which includes sales price plus tax or more-likely a method .
7th Jan 2019, 4:12 AM
Instructor M
Instructor M - avatar
0
ok thanks let me try one more time .
7th Jan 2019, 4:45 AM
Instructor M
Instructor M - avatar
0
self.price_plus_tax = self.price + (self.price * self.tax) made this change . still see this error when i run File "..\Playground\", line 16 def displayAll(self): ^ IndentationError: unindent does not match any outer indentation level
7th Jan 2019, 7:52 PM
Instructor M
Instructor M - avatar
0
Hello , i am having a little bit of trouble Price: 5000 Speed: 40 mph Fuel: Half Full Mileage: 10000 mpg Tax: 0.1 price_plus_tax: 5500.0 Price: 15000 Speed: 100 mph Fuel: Full Mileage: 65000 mpg Tax: 0.15 Traceback (most recent call last): File "..\Playground\", line 37, in <module> car2 = Car(15000, 100, 'Full', 65000) File "..\Playground\", line 12, in __init__ self.displayAll() File "..\Playground\", line 32, in displayAll print ('price_plus_tax: ' + str(self.price_plus_tax)) AttributeError: 'Car' object has no attribute 'price_plus_tax'
8th Jan 2019, 6:31 AM
Instructor M
Instructor M - avatar