rgkbitw's blog

By rgkbitw, history, 7 years ago, In English

I came across this problem in UVa OJ. 272-Text Quotes

Well, the problem is quite trivial. But the thing is I am not able to read the input. The input is provided in the form of text lines and end of input is indicated by EOF.

In C/C++ this can be done by running a while loop:

  while( scanf("%s",&s)!=EOF ) { //do something } 

How can this be done in python .?
I have searched the web but I did not find any satisfactory answer.

Please Help Me!
  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

in python 3:

while True:
    try:
        s=input()
        #do something
    except EOFERROR:
        break
  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Using exception handling is expensive and might cause an overhead. Is there any simpler way?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I always use it whenever i submit in UVA IN PYTHON. However you can take a look here..but I think you did..

»
7 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Just iterate over the input.

for line in sys.stdin:
    process_line(line)
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

That's why you don't do UVa (if possible) :P.