Python EOF error raw_input()

坚强是说给别人听的谎言 提交于 2019-11-27 04:53:10

问题


I'm using Zed Shaw's Learn Python the Hard Way.

In exercise 11, the code produces an EOF error on line 2. Here is the code:

1  print "How old are you?",
2  age = raw_input()
3  print "How tall are you?",
4  height = raw_input()
5  print "How much do you weigh?",
6  weight = raw_input()
7  print "So, you're %r old, %r tall and %r heavy." % (
       age, height, weight)

I have searched StackOverflow, Google, and Hacker News forum. I could not find any answer that (a) solved this problem and (b) I could understand.

I am using the python compiler on ideone.com (have also tried two other on-line compilers and neither worked).

(Read this but could not apply it: Python EOF Error in raw_input()).


回答1:


The problem is that online interpreters usually don’t pause to allow the user to input stuff. Instead, they will use a fixed “file” as stdin from which the data is read. Unless you specify it, it will be empty, so asking for input will result in EOF since the (empty) file was already exhausted.

It is possible to specify the input data though. On ideone, you have to click the stdin button and enter the data at once. For example:

old
tall
weight

Then your script will run.

But you really should consider downloading Python yourself and running it in the command line with a normal interpreter. That way, you actually get some interactivity.



来源:https://stackoverflow.com/questions/23079388/python-eof-error-raw-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!