+ 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

2 Answers

+ 3

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)

+ 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

Hot today

What is indentations... ?

4 Votes

Application RAM usage

1 Votes

Showing errors!

2 Votes

Pandas Problem

4 Votes