while continue in LIST | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

while continue in LIST

hi all I try to do the example proposed in while loops lesson, ###An example use case of continue: An airline ticketing system needs to calculate the total cost for all tickets purchased. The tickets for children under the age of 1 are free. We can use a while loop to iterate through the list of passengers and calculate the total cost of their tickets. Here, the continue statement can be used to skip the children.### but I can't find the good code, if someone can help me I will be very thankful :D there is the code i made Intel know :( sorry for my English not very good ) coste_age =[20,2,58,1,89,56,1] x=len(coste_age) i=0 while i<x: if cost_age[i] <=1: continue print (cost_age[i]) i+=1 print (coste_age[i]) i+=1

9th Apr 2021, 11:08 PM
Chima Mima
Chima Mima - avatar
2 Answers
+ 4
The indentation is off which will mess up the Python. What is the cost for people over 1 year of age? Assuming the cost of each ticket for someone over 1 year of age is $1, this will work: cost_age =[20,2,58,1,89,56,1] x=len(cost_age) i=-1 total_cost = 0 while i < x - 1: i += 1 if cost_age[i] <= 1: continue total_cost += 1 print('Total cost for all tickets is: $%d.' % total_cost) My interpretation of this statement is that only babies under 1 year of age are free: "The tickets for children under the age of 1 are free" In other words, it would be "cost_age[i] < 1" instead of "cost_age[i] <= 1". If you weren't encouraged to use a while-loop, I would use a for-loop instead like this to avoid defining variable i. cost_age =[20,2,58,1,89,56,1] total_cost = 0 for age in cost_age: if age <= 1: continue total_cost += 1 print('Total cost for all tickets is: $%d.' % total_cost)
9th Apr 2021, 11:30 PM
Josh Greig
Josh Greig - avatar
+ 1
thx a lot for your help know i understand where was my error i forgot to give a variable total_cost i just focus in 1 , thx again
10th Apr 2021, 7:35 AM
Chima Mima
Chima Mima - avatar