Why does not print return all items in a list?

-2

The following code should return all cars from the following list:

cars = ['audi','bmw','subaru','toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
else:
    print(car.title())

But just returned: BMW Toyota

What's wrong?

    
asked by anonymous 12.06.2018 / 02:37

1 answer

1

Your formatted code is as follows:

cars = ['audi','bmw','subaru','toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
else:
    print(car.title())

The problem is that else is at the wrong indentation level. It's in your code referring to for . You want it to be at the same indent level as if :

cars = ['audi','bmw','subaru','toyota']

for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

# Audi
# BMW
# Subaru
# Toyota
    
12.06.2018 / 02:42