Reputation: 13
I am new to Python and would like to create a print function that repeats itself for each item in a list (this is only an example my actual code will be for something else)
cars.list = [Honda, Chevrolet, Suzuki, Ford]
price.list = [5600, 11500, 6600, 1020]
The prices and car lists are in the same order so Honda is $5600, Chevrolet is 11500 ect. I'd like it to run a loop for each of the cars that prints this:
while count in cars.list:
print("The car type is" Honda/Chev ect. "The price is" 5600, 11500 ect"
I want it to repeat the loop for as many cars are in the cars.list
, as I will add an option for the user to add more cars so the program cannot rely on knowing the specific cars in a list and copying the print statement for each one. It needs to repeat the print statement for every car, replacing the price and car type each time with the next one in the list.
Upvotes: 0
Views: 296
Reputation: 8610
You can use zip
to associate the cars and prices together in an iterator of tuples, then iterate over those tuples while printing what you want.
cars = ['Honda', 'Chevrolet', 'Suzuki', 'Ford']
prices = [5600, 11500, 6600, 1020]
for car, price in zip(cars, prices):
print('The car type is {} and the price is {}.'.format(car, price))
Output:
The car type is Honda and the price is 5600.
The car type is Chevrolet and the price is 11500.
The car type is Suzuki and the price is 6600.
The car type is Ford and the price is 1020.
Upvotes: 1