+ 1

26.3 Ticket Prices Python

Hello, I’m having trouble with this code. I keep getting an error about age = input(), unless I put break where continue is. If I put break there, two of the tests are satisfied, but it breaks whenever someone younger than 3 is buying a ticket. total = 0 #your code goes here while True: age = input() if int(age) >= 3: total +=100 if int(age) < 3: total +=0 continue print(total)

22nd Feb 2021, 9:55 PM
Ry Guy
17 Answers
+ 8
It turns out when the file ended, there was no input resulting in an error in the loop (end of file error or EOFError). To fix this you can use try and except or you can set a limit on the loop. See below if you want the code. Turns out there are a couple ways to do this: 1) If you want as many passengers as want to get on board: total = 0 #your code goes here while True: try: age = input() except EOFError: break if int(age) >= 3: total +=100 if int(age) < 3: total +=0 continue print(total) 2) You only want to allow 5 passengers total = 0 p = 1 #your code goes here while p <= 5: age = input() if int(age) >= 3: total +=100 p +=1 if int(age) < 3: total +=0 p +=1 continue print(total)
22nd Feb 2021, 10:58 PM
Ry Guy
+ 6
total = 0 people = 0 while people < 5: age = int(input()) if age > 3: total +=100 people += 1 else: people +=1 print(total) just wanted to help, and it works
1st Dec 2021, 5:35 PM
Eslam Idris
Eslam  Idris - avatar
+ 3
My code looks like that: total = 0 i = 0 while i < 5: age = int(input()) i += 1 if age > 3: total += 100 print(total) I only check when age is bigger than 3 because .. it’s clear that everything else will be smaller than 3. I don’t have to add zeros.
28th Apr 2021, 7:27 PM
Marek Marczak
Marek Marczak - avatar
+ 2
My code was throwing eof errors saying it was the input line, but the actual problem was that I accidentally set the number of iterations from 0-5 which Is one more than the program entered. When I changed p to 1 instead of zero or 5 to 4 that changed my iterations to 5 and it worked.
5th Jan 2022, 6:12 PM
Travis Beard
Travis Beard - avatar
+ 2
total = 0 i = 0 while i < 5 : age = int(input()) if age > 3 : total = total + 100 i += 1 print(total)
3rd Feb 2022, 12:04 PM
树上的鱼
树上的鱼 - avatar
+ 1
Hi! Hint: in the age variable, you use input and it is saved as a string. and you need to save it as a number...
22nd Feb 2021, 10:05 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Here is the way I found: total = 0 #your code goes here count = 0 while count <= 4: age = input() if int(age) >= 3: total += 100 count += 1 else: count += 1 continue print (total)
8th Mar 2021, 10:42 PM
Ferrer y Ponce SL
Ferrer y Ponce SL - avatar
+ 1
I wrote this code and it is working. total = 0 #your code goes here i=1 while i<6: age=int(input()) if age<3: i+=1 continue else: total+=1 i+=1 price=int(total)*100 print(price)
15th Jul 2021, 8:02 AM
Sushant Kashikar
Sushant Kashikar - avatar
+ 1
Age=int(input()) Total=0 While(age>=0): If(age<=3): Total+=0 Elif(age>3): Total+=100 Print (total) break
20th Sep 2021, 4:58 AM
Chandra Sekhar
+ 1
it's the shortest ☺️ total = 0 i=1 while i<=5 : age=int(input()) i+=1 if age<3: continue total+=100 print(total)
23rd Oct 2021, 8:24 AM
Mahdi Bitaab
Mahdi Bitaab - avatar
+ 1
I have tried several ways including many described here to no success. This is my code today: total = 0 #your code goes here passengers = 1 while passengers <= 5: try: age = input() except EOFError: if int(age) >= 3: total += 100 passengers += 1 if int(age) < 3: total += 0 passengers += 1 continue print(total) None of the test cases passes. Any pointers?
24th Oct 2021, 11:11 PM
Javier Cabeza
+ 1
I have the same issue: price = 100 total = 0 for i in range(5): age = int(input()) if age > 3: total += price elif age <= 3: continue print(total) Can someone point out where the error is???
5th Dec 2021, 9:47 PM
Salim Nyali
+ 1
why do we have to type age=int(input()) after while syntax?
14th Jan 2022, 10:00 PM
Fedor Vasilev
Fedor Vasilev - avatar
+ 1
and why do we have to limit the number of passengers to 4 if there can be 5 of them?
14th Jan 2022, 10:05 PM
Fedor Vasilev
Fedor Vasilev - avatar
+ 1
Please where is the correct answer am also having problem with that one
2nd Mar 2022, 11:40 PM
Ampong Godfred
Ampong Godfred - avatar
0
Mine is shorter but works: total = 0 p = 0 while p < 5: x = int(input()) if x > 3: total += 100 p += 1 else: total += 0 p += 1 print(total)
11th Mar 2021, 1:30 PM
Luiza
Luiza - avatar
0
passenger=0 sum=0 while passenger<5: passenger+=1 age=int(input()) if age>3: sum+=1 else: continue print(sum*100) #thats works successfully
23rd Aug 2024, 12:23 PM
mohamed nasser