+ 1

I'm getting an error message on my solution and I don't know why

I'm working on the code coach Ticket Prices. Here is my attempt at solving it: a = 0 total = 0 while True: a += 1 x = int(input()) if x > 3: total += 100 if x <= 3: continue if a == 5: break print(total) For two of the test cases, my solution works. For the other two test cases, it says: Traceback (most recent call last): File "/usercode/file0.py", line 5, in <module> x = int(input()) EOFError: EOF when reading a line Any idea why this happens and how to work around it?

29th Aug 2021, 9:46 PM
Alex
Alex - avatar
5 Antworten
+ 3
as Simon Sauter suggested, you need 5 inputs. in sololearn only one input screen is shown, even if you have multiple inputs. additionally, you can remove the ‘if x<=3: continue’ condition from your code. below is an example: a = 0 total = 0 while True: a += 1 x = int(input()) if x > 3: total += 100 if x <= 3: # dont need this condition continue if a == 5: break print(total) """ input: 1 2 3 4 5 output: 200 """
29th Aug 2021, 10:49 PM
you are smart. you are brave.
you are smart. you are brave. - avatar
+ 2
If x happens to be equal or less than three in the iteration where a equals five the break statement is never executed and you get an infinite loop requiring an infinite number of inputs. Maybe that condition is fulfilled in one of the test cases. That would explain why removing the x <= 3 condition solved the issue.
30th Aug 2021, 12:30 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Your code requires a minimum of five inputs. Maybe those test cases have less.
29th Aug 2021, 10:02 PM
Simon Sauter
Simon Sauter - avatar
+ 1
you are smart, Removing ‘if x <= 3: continue’ fixed the code. Idk why, but the error message doesn’t appear anymore. So thanks for the suggestion
30th Aug 2021, 12:11 AM
Alex
Alex - avatar
0
Simon Sauter it took me a while to get a grasp of what you were saying, but i finally caught on. you are right about the infinite loop because on the fifth iteration and if x is less than or equal to 3, it will “continue” which will take the loop back to the beginning completely skipping the if x==5 condition statement. i guess if you really wanted to keep that their, it would be better to use “pass” instead of “continue”. either way its better to remove it all together.
30th Aug 2021, 4:33 AM
you are smart. you are brave.
you are smart. you are brave. - avatar