+ 1

Can u tell me why my version doesn't work but the second one works?

Task is: continue You are making a ticketing system. The price of a single ticket is $100. For children under 3 years old, the ticket is free. Your program needs to take the ages of 5 passengers as input and output the total price for their tickets. Sample Input 18 24 2 5 42 Sample Output 400 My try that doesn't work is: ticket = 100 total = 0 passengers = 5 while passengers >= 0: age = int(input()) passengers -= 1 if age < 3: continue else: total += ticket print(total) this one works, but why? ticket = 100 total = 0 passengers = 1 while passengers <= 5: age = int(input()) passengers += 1 if age < 3: continue else: total += ticket print(total) I already noticed that I can cut out the else part in the working one to this code: ticket = 100 total = 0 passengers = 1 while passengers <= 5: age = int(input()) passengers += 1 if age > 3: total += ticket print(total)

7th Sep 2021, 4:15 PM
Kobe
Kobe - avatar
5 Answers
+ 4
Hi Kobe! Your updated code works fine here too. But Sololearn code playground works little different than other ides. In that alert(input)box, we have to provide all inputs at one time in a new line. For example, 18 24 2 5 42 Now hit the submit ticket = 100 total = 0 passengers = 5 while passengers > 0: age = int(input()) passengers -= 1 if age > 3: total += ticket print(total)
7th Sep 2021, 4:46 PM
Python Learner
Python Learner - avatar
+ 3
Kobe Kramer Because you have written >= 0 and your variable passengers declared with 5 so while loop will work 6 times. Change >= 0 to > 0
7th Sep 2021, 4:19 PM
A͢J
A͢J - avatar
+ 2
Kobe you have a logic error in your second code attempt. You use the test if age >3:, but in the challenge it says children under 3 are free. Which means a ticket for a child that is 3 should be $100.
7th Sep 2021, 4:45 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
Ahhhh, thank you! Was confused why it works in other programs :D
7th Sep 2021, 5:04 PM
Kobe
Kobe - avatar
0
Thanks I saw that after I posted it :D But it doesn't even run. It says "Traceback (most recent call last): File "/usercode/file0.py", line 5, in <module> age = int(input()) EOFError: EOF when reading a line" I tested it in PyCharm but the code works there. Any ideas? Changed the 6th input. This works in PyCharm but not here: ticket = 100 total = 0 passengers = 5 while passengers > 0: age = int(input()) passengers -= 1 if age > 3: total += ticket print(total)
7th Sep 2021, 4:27 PM
Kobe
Kobe - avatar