Home > Software design >  Is there any way to call particular items in a list using their index number inn python?
Is there any way to call particular items in a list using their index number inn python?

Time:02-20

I am asking this in relation to a project I am working on, highlighted in my previous question. I have the following code to ask questions and take the input as answers, logging them in a separate list:

     questions = ["Please enter your car's brand name: ", "Please enter your car's model name: ", "Please enter your car's year: ","Please enter yes/no for whether or not your car has scratches/dents: ","Please enter quoted price given by representative: "]
     answers = []
     for q in questions:
         a = input(q)
         answers.append(a)
     print(a)

The problem is, I get None, None as the answer as None None Whenever it is run in conjunction with the code I am working on. Please point out any mistake I am making, as, on its own, the code works the way I want it to work in the aforementioned other code.

Edit: perhaps I should clarify: I am looking for a way to enter the individual elements into a table in MySQL via the connector. I have the following code hashed out, but I am struggling with actually entering the elements:

sql = "insert into new_purchases \nValues('element1','element2','element3','element4');"

Here, elements 1-4 are the elements of list answers and are being entered into the table new_purchases.

CodePudding user response:

YES, You definitely call a particular item in a list by using their index number.

Minor fix in your code.

questions = ["Please enter your car's brand name: ", "Please enter your car's model name: ", "Please enter your car's year: ","Please enter yes/no for whether or not your car has scratches/dents: ","Please enter quoted price given by representative: "]
answers = []
for q in questions:
    a = input(q)
    answers.append(a)
print(answers)

if you call a particular item in a list then simply use this code snippet.

index = 0 # 0, 1, 2, ...., n
print(questions[index])

CodePudding user response:

Yes, you just need to replace the print(a) with print(answers).

  • Related