Why can't I use for loop to print member variables?


#1



Classes - Referring to Member Variables


I can correctly pass this lesson by copy-pasting. I want to know if I can use a for loop to print.


class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg

my_car = Car("DeLorean", "silver", 88)
print(my_car.condition)

### This will pass just fine. ###
print(my_car.model)
print(my_car.color)
print(my_car.mpg)

### Why doesn't this work? ###
for i in ["model", "color", "mpg"]:
    print(my_car.i)


#2

Link to the exersice, please.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.