0

Why its so!?

The task is: **** 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 ***** The code is: ***** total = 0 i = 0 while i < 5: age = int(input()) i+=1 if age < 3: continue else: total += 100 print(total) ****** I don't understand just 1 thing. Why i += 1 is below age = int(input)? Why if it is in the end of cycle (under total += 100) we get EOF error? And additional question (not necessary to answer): Is it possible to do this task using endless cycle (while True)

23rd Feb 2023, 9:22 PM
Vlonico
Vlonico - avatar
1 Réponse
+ 3
The task adds 5 inputs to your code when running.. So you need to accept 5 inputs exactly. When you add i+=1 below total, it won't get incremented when input age <3 because of continue execution so then loop runs one more extra time and asks one more input. There will ne no inputs above 5th time so it raise error. Using while True : you can use it but make sure you start loop after 5 inputs. By break when if i >= 5 : break Or you can use try catch, but those are all not better ways according to task.
23rd Feb 2023, 9:33 PM
Jayakrishna 🇮🇳