Dec-17-2021, 03:43 PM
Hello all. I'm currently taking an Intro to Python course, and I'm on the lesson where we're dealing with Class composition. In the exercise, I have two Py scripts:
creating_classes.py:
Thanks for looking!
creating_classes.py:
class Car:
"""
Car models a car w/ tires and an engine
"""
def __init__(self, engine, tires):
self.engine = engine
self.tires = tires
def description(self):
print(f"A car with an {self.engine} engine, and {self.tires} tires")tire.py:class Tire:
"""
Tire represents a tire that would be used with an automobile.
"""
def __init__(self, tire_type, width, ratio, diameter,
brand='', construction='R'):
self.tire_type = tire_type
self.width = width
self.ratio = ratio
self.diameter = diameter
self.brand = brand
self.construction = construction
def __repr__(self):
"""
Represent the tire's information in the standard notation present
on the side of the tire. Example: 'P215/65R15'
"""
return (f"{self.tire_type}{self.width}/{self.ratio}"
+ f"{self.construction}{self.diameter}")The video in the code (where the instructor is using Python 3.7) shows the output as follows:Output:$ python3.7 -i creating_classes.py
>>> from tire import Tire
>>> tire = Tire('P', 205, 55, 15)
>>> tires = [tire, tire, tire, tire]
>>> honda = Car(tires=tires, engine='4-cylinder')
>>> honda.description()
A car with a 4-cylinder engine, and [P205/55R15, P205/55R15, P205/55R15, P205/55R15] tiresHowever, when I run the script (using both Python 3.9 and 3.10), I get the following:Output:(learning_python) learning_python $ python3.10 -i creating_classes.py
>>> from tire import Tire
>>> tire = ('P', 205, 55, 15)
>>> tires = [tire, tire, tire, tire]
>>> honda = Car(tires=tires, engine='4-cylinder')
>>> honda.description()
A car with a 4-cylinder engine and [('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15), ('P', 205, 55, 15)] tiresAny idea why the output is formatted this way, as opposed to the way it should be, with proper formatting in the fstring?Thanks for looking!
