Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

raw input - Python EOF error raw_input()

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()).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...