0

I appended a list into another list, via loop. But I want to access the individual inputs, so I can print them line by line. I am attempting to make an inventory sheet for cars, but I want it to look neat

car = []
cars = []
add = "yes"
loopcars = 0
loopcar = 0

while(add == "yes"):
    make = input("Make: \n")
    model = input("Model: \n")
    year = input("Year: \n")
    miles = input("Miles: \n")
    price = input("Max bid: \n")
    car = [make, model, year, miles, price]
    cars.append(car)
    add = input("Add a Vehicle?(yes or no)")

while(loopcars != len(cars)):
    loopcars = loopcars + 1
    while(loopcar != len(cars[loopcar])):
        print (cars[loopcar])
        loopcar = loopcar + 1

This is the output

Make: 
Chevy
Model: 
Impala
Year: 
2005
Miles: 
125000
Max bid: 
3500
Add a Vehicle?(yes or no)yes
Make: 
Ford
Model: 
Escape
Year: 
2010
Miles: 
127000
Max bid: 
4500
Add a Vehicle?(yes or no)no
['Chevy', 'Impala', '2005', '125000', '3500']
['Ford', 'Escape', '2010', '127000', '4500']

I want it to list out like:

Chevy
Impala
2005
125000
3500
etc...
  • 1
    You really should be using for-loops here. – juanpa.arrivillaga Aug 15 '17 at 20:16
  • This looks like a good opportunity to make [car objects](https://stackoverflow.com/a/1164319/7795130), then your list would be a list of cars. [Python, creating objects](https://stackoverflow.com/questions/15081542/python-creating-objects) – Davy M Aug 15 '17 at 20:19
  • 1
    @DavyM baby steps... Probably should learn basic control-flow and how to use a for-loop before learning how to write a class definition... – juanpa.arrivillaga Aug 15 '17 at 20:22

4 Answers4

2

Replacing the second while loop

for each in cars:
    for tmp in each:print(tmp)
vintol
  • 48
  • 4
1

This is really (bad) C-like code. As stated in the first comment, the last part of your code should use for-loops. It is useful to avoid tricky bugs like the ones in your code:

while(loopcars != len(cars)):
    loopcars = loopcars + 1
    # loopcars is never used after here
    while(loopcar != len(cars[loopcar])): # SHOULD BE len(cars[loopcars-1])
        print (cars[loopcar]) # SHOULD BE cars[loopcars-1][loopcar] AND NOT cars[loopcar]
        loopcar = loopcar + 1

What happens here?

  • cars[loopcar] does not make sense ;
  • print (cars[loopcar]) will a best print the list of properties (the "car") instead of a property.

Now look at this code:

for loopcars in range(len(cars)): # loopcars = 0,1,...len(cars)-1
    car = cars[loopcars]
    for loopcar in range(car): # loopcar = 0,1,...len(car)-1
        print (car[loopcar]) 

It is still C-like (the for loop is a bit different, but the style is the same). It is also neater, and the bugs have disappeared.

But you are using Python, and you could do better:

for car in cars:
    for prop in car:
        print (prop)

This is prettry clear now. The for-in loop on a list walks through the elements of that list. You walk trough the cars, and for each car, you walk through its properties (and print each property).

My advice: there are a lot of good Python tutorials, and you should follow one.

jferard
  • 7,835
  • 2
  • 22
  • 35
0

You can use for loop within a for loop to get each attribute of a car on new line.

Here is how I would simplify the code

cars = []

while True:
    make = input("Make: \n")
    model = input("Model: \n")
    year = input("Year: \n")
    miles = input("Miles: \n")
    price = input("Max bid: \n")
    cars.append([make, model, year, miles, price])
    cont = input("Another one? yes/no > ")
    if cont == "no":
      break

for car in cars:
  for attribute in car:
    print(attribute)
  print "-------"

Example:

$ python test.py
Make:
Chevy
Model:
Impala
Year:
2005
Miles:
123
Max bid:
3500
Another one? yes/no > yes
Make:
Ford
Model:
Escape
Year:
2010
Miles:
127000
Max bid:
4500
Another one? yes/no > no
Chevy
Impala
2005
123
3500
-------
Ford
Escape
2010
127000
4500
vaichidrewar
  • 9,251
  • 18
  • 72
  • 86
0
cars = [
    ['Chevy', 'Impala', '2005', '125000', '3500'],
    ['Ford', 'Escape', '2010', '127000', '4500']
    ]

for car in cars:
    for item in car:
        print(item)

You might want to have a look at for loops (or iteration in python in general)

MathiasL
  • 24
  • 3