python-3.xeoferror

EOFError, even after trying the try and except block


Could anyone please state the reason why it is showing. I acknowledge it as python after reading the input states that there is nothing to read.

Python 3.6

    #!/bin/python3

import math
import os
import random
import re
import sys

while True:
    try:
        N = int(input())
    except EOFError:
        return
#N = int(input())


if N % 2 != 0:
    print("Wierd")
elif N % 2 == 0 and N in range(2, 6):
    print("Not Wierd")
elif N % 2 == 0 and N in range(6, 21):
    print("Wierd")
elif N % 2 == 0 and N > 20:
    print("Wierd")



if __name__ == '__main__':
    N = int(input())

the error statement

Traceback (most recent call last):
  File "solution.py", line 27, in <module>
    N = int(input())
EOFError: EOF when reading a line
Blockquote

Solution

  • return only works when you are inside a function and you want to exit from that function. In this case you just want to terminate your while loop so your should use break keyword.

    Also in given question, you just need to read a single integer (I guess you are working on something different?)

    import math
    import os
    import random
    import re
    import sys
    
    while True:
        try:
            N = int(input())
        except EOFError:
            break
    #N = int(input())
    
    
    if N % 2 != 0:
        print("Wierd")
    elif N % 2 == 0 and N in range(2, 6):
        print("Not Wierd")
    elif N % 2 == 0 and N in range(6, 21):
        print("Wierd")
    elif N % 2 == 0 and N > 20:
        print("Wierd")
    
    
    
    if __name__ == '__main__':
        N = int(input())