I have to create a list of cars, a function to display the cars but replace the fourth car with a phrase, and call the function, for an assignment. One of the parts of the grading scheme is to use a for statement to iterate, not a while loop. It also says that it can be done in under 10 lines of code.
I have created a while loop that does prints the proper list, but I cannot manage to change it to a for statement successfully.
def my_Assign3_challenge(cars):
i=0
while i<len(cars):
print(cars[i])
i=i+1
if i==3:
print("I would never buy that!")
i=i+1
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)
This for statement just prints the list, without the change:
def my_Assign3_challenge(cars):
for x in cars:
if x!= 3:
print(x)
else:
print("I would never buy that!")
cars=["toyota","mitsubishi","dodge","ford","mini"]
my_Assign3_challenge(cars)
I also managed a couple times to have just the first list item repeat indefinitely, I can't find the code that did that now, though.
I would greatly appreciate if anyone could take a look at this and tell me where I went wrong. I'm sure it's just a simple mistake, as I am very new to coding, but I would love your help!