I'm trying to solve a problem using the following code:
X,Y = map(float, input().split())
if X < Y and not X % 5:
print(Y - X - 0.50)
else:
print(Y)
This code gives me the desired output when I run using IDLE. However, when I try running this code using an interpreter provided by a competitive programming website, I get the following error:
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line
I tried reading the answers of other similar questions, but none of them seemed to work in my case.
I am not sure of the reason but the program is trying to read after the end of the data. You can solve the problem by exception handling
try:
data = input()
except EOFError:
break
Take another look at the codechef page. Notice the checkbox marked 'Custom Input'. With that checked/ticked a textbox will open where you can put your input lines.
The competitive programming website is likely running python 2. Python 2 treats input() differently than python 3.
You should rather use raw_input() than input().
From the docs:
raw_input() reads a line from input, converts it to a string
(stripping a trailing newline), and returns that.
Your problem can be explained from what was explained here:
In Python 2, raw_input() returns a string, and input() tries to run
the input as a Python expression.
Related
The following code runs fine on PyCharm but the CodeChef IDE simply won't take it. Am I missing something here? Please convince me I'm a fool for sitting on this all day
I even tried switching to Python 2.7 and changing the functions to raw_input().
n = int(input()) # Line 1. This is where it goes bananas
I get the desired Output on PyCharm
But, on CodeChef:
Runtime Error: NZEC
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line
Focus on EOFError: EOF when reading a line
It happens because the code needs an input from command line, but the file that provides the input is empty.
n =int(input())
consider the above line. It'll expect an input but it will get EOF delimiter as soon as it reads the file.
Hence, raising an EOFError.
Insist of using input() method to take input from user you can use stdin(standard input).
Simply import sys.stdin and use n=eval(stdin.readline()) to take input. Also put your code inside try and execpt EOFError.
Sample -
from sys import stdin
try:
n=eval(stdin.readline())
# write your code here
except EOFError:
print("EOFError")
I always get this error whenever using input() in Python3 in any online compiler, hackerrank, wipro portal, interviewbit etc. I've seen so many posts regarding this, but none of them is working for me. try except block leads to always execution of the except block which I don't want as I'm still not able to read any input.
Even as simple as the following code doesn't work. Help.
b = int(input())
print (b)
I get the following error:
Traceback (most recent call last):
File "main.py", line 227, in
Z = obj.solve(A)
File "/tmp/judge/solution.py", line 9, in solve
b = int(input())
EOFError: EOF when reading a line
Try going to https://www.hackerrank.com/challenges/python-loops/problem where the single input line is already there in the starter code for you. If you select Python 3 from the language dropdown, and then -- without entering any code of your own at all -- click Run Code, you should get a Wrong Answer "no response on stdout" response. Do you get that, or do you still get an EOFError? I'm assuming you don't get the EOFError and that perhaps there's a problem with where/how you're entering your code into their editor.
If you're getting an error like that with InterviewBit, I'd say it's because with InterviewBit you're not supposed to be reading from stdin at all -- the starter code will have a function that their test cases call, and then you complete the function code to return the desired output.
I am following zed shaw's book LPTHW and stuck on ex25. I have typed the code accordingly without errors. If I understand it right he has asked to run it first normal in powershell but when I put the command:
C:/mystuff> python ex25.py ,
It enters to next line without any output.
I tried importing ex25 in python interpreter this is the error i get:
>>import ex25
>>sentence = "All good things come to those who wait."
>>words = ex25.break_words(sentence)
After this I get an error:
Traceback <most recent call last>:
file stdin in line 1 <module>
file ex25.py line 3 in break_words
words = stuff.split(' ')
Value error: empty separator.
what am I doing wrong? Also i experimented for over half hour now trying different solutions , make it work. I guess the powershell wont respond because the program is basically a bunch of functions , without any input. But in the interpreter we do give an input of a sentence , then why the error? Doing my head-in.
the code link for LPTHW: http://learnpythonthehardway.org/book/ex25.html
I don't know your code for ex25 but here is mine for the line it says you have error on
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
It works for me you can make a new file and put that portion of your code and mine then compare them if they are exactly the same with 0 difference then tell me. if you can can you post that part of your code in the question
I would like to test programs online, as the place where I work does not have a python compiler. There are many online sites such as ideone, codepad and complieonline. However, I have noticed that codepad does not accept input data and although ideone input data, it only accepts one entry data at a time. For example I wanted to test this program on complileonline
PREFIX = 'Simon says '
line = raw_input('Enter: ')
while line:
if line.startswith(PREFIX):
print line[len(PREFIX):]
line = raw_input('Enter: ')
I entered my input and separated them by pipeline (as stated on the website instructions) but kept getting this error message
Executing the code....
$python /tmp/135731949523855.py
Enter: Enter: Enter: Traceback (most recent call last):
File "/tmp/135731949523855.py", line 9, in ?
line = raw_input('Enter: ')
EOFError: EOF when reading a line
How can I enter multiple entries to test my program? How can I enter multiple entries onto these online websites, otherwise is there another, example testing with my code without the requirement of user input.
Well, consider to use hard-coded inputs inside your code (turn the "raw_input" function into something you except to get from the user). It can be great solution.
One of the options you have which is pretty close to raw_input, is to use sys.argv:
import sys
print sys.argv[1]
Compileonline provides you the option, below of your code, to add command line arguments.
However, on PythonAnywhere.com your code seems to run just fine :) You might want to try it there.
Good luck:)
I'm reading an online python tutorial book from here. The code is listed below. When I execute the code, I can type words into it but then it gave me the error below. What is the wrong with the code?
On a related note, if you have a better resource for leaning python, please let me know. I'm looking for one that is online and updated often (ex: railstutorial.org). The resource I am using have plenty of errors even this early in the book. Thanks.
Enter something : programmig is fun
Traceback (most recent call last):
File "break.py", line 5, in <module>
s = input('Enter something : ')
File "<string>", line 1, in <module>
NameError: name 'programmig' is not defined
#!/usr/bin/python
# Filename: break.py
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
This is python 3 code. It seems like you're running it with python 2.
Run python --version to check which version of python you are using.
input() doesn't get a string, so it thinks that programmig is a variable. You can type the input you want in quotes to solve this.
A better way however, is to use raw_input, which returns a string.
So either do Enter something : 'programmig is fun', not recommended, or do s = raw_input('Enter something : ') recommended way
The cause of the confusion is that the book is probably for python 3, which has a different input, and also a different print, while you are using python 2.x.