Ishtiaq11's blog

By Ishtiaq11, history, 8 years ago, In English

If the amount of data is unknown, the following loop is useful in c++:

 while (cin >> x) {
 // code
 }

How to do this in Python?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it
import sys 
for line in sys.stdin: 
  #type your code here
  • line is a string from stdin.
  • line contains '\n'.
  • Don't forget to use strip().