+ 1
Why is the output so high?
https://www.sololearn.com/en/compiler-playground/cAVQKC5I1bvj
8 Réponses
+ 3
you are compounding ticket_price.
Perhaps you mean to compute total_price?
nr_tickets = 0
ticket_price = 161
# calculate total of all tickets sold to adults
# the list simulates tickets sold to children
kids = [37, 63, 41, 17, 39, 4, 44, 73, 18, 81, 82, 64, 51, 53, 72, 97, 50, 89, 11, 98, 42]
total_price = 0   # use this
while nr_tickets <= 99:
    nr_tickets += 1
    if nr_tickets in kids:
        continue    
    total_price += ticket_price
 # use here
print(total_price)  # what you want
https://www.sololearn.com/compiler-playground/cAVQKC5I1bvj/?ref=app
+ 2
some comments to your current code:
>> we need:
    > variable 'ticket_price'
    > variable like 'total_price'
>> we do *not* need:
    > variable 'nr_tickets'
    > while loop
>> we should:
    > rename the list 'kids' to -> 'ages', because the values in this list are ages
> for iterating through a list it is better to use a for loop. it runs through the list
    until all items from the list have been given to the loop variable. 
    this is done one at a time.
> compare the content of the loop variable  in each iterating step against the
   condition ??? less than the age of 1 ???. (i remember this task, but in my code
   i did for this the condition for children was less than 3)
    > if the condition is true, skip this item.
    > otherwhise add the variable 'ticket_price' to the 'total_price'.
> after all items have been picked by the for loop, we can output the 
    'total_price' variable
+ 2
Lothar the list contains serial numbers of tickets, not ages of passengers. Number of passengers is 99, as it can be seen from the line 
while nr_tickets <= 99
It's an example from a While Loops lesson that i wanted to use for a code.
+ 2
Igor Matić 
I think Lothar  means while your code works fine, it's not 'Pythonic'.  
The while loop is not the usual 'Pythonic' iteration method. 
for and range is what I would think of using out of habit.  I usually use the while loop in Python for infinite loops, or when a for loop is not suitable. It's a common pattern. 
Not saying you should, but perhaps to make your code more  conventionally Pythonic. People reading codes can usually tell if you're into Python or not...😁
maybe something like:
ticket_price = 161
kids = [37, 63, 41, 17, 39, 4, 44, 73, 18, 81, 82, 64, 51, 53, 72, 97, 50, 89, 11, 98, 42]
total = 0
for n in range(100):
  if n not in kids:
    total += ticket_price
    
print(total)
+ 1
Bob_Li what does compounding means in this case?
+ 1
Igor Matić 
You are continually adding ticket price to itself. The value will snowball. You're only supposed to add 161 every time.
What's happening in your code:
ticket_price = 161
#example only
i = 0
while i <=10:
    i += 1
    ticket_price += ticket_price # don't do this
    print(ticket_price) # crazy value.. 🤯
+ 1
I see Bob_Li , thank you for the explanation



