0
import sys

n = int(sys.stdin.readline())

for _ in range(n):
   s = input()
   print(s)

This is my code in Python3.3.

I encounter with an input problem.

If i enter my input by one by one first '3' and first string and second string and third string.

There is no problem. But when I copy all input data and paste it readline() function reads all data. I thought it reads until '\n',

But it is not working.

Here is the Error Message :

Traceback (most recent call last):
  File "D:/Users/SUUSER/Desktop/a.py", line 3, in <module>
    n = int(sys.stdin.readline())
ValueError: invalid literal for int() with base 10: '3\nasd\nfre\n345\n'
'3\nasd\nfre\n345\n' 

It reads all data for (n)

But I just want to read 3.

How can i get it ?

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
bahadir
  • 3
  • 1
  • 1
    That is very strange - the code works fine for me. How are you running the Python script? – icedtrees Mar 28 '14 at 08:30
  • Why not use `n = int(input())` there? – Martijn Pieters Mar 28 '14 at 08:31
  • Same here, works for me too. Could you perhaps record the incident (Would be nice to see how you actually input the data, where, what console, OS etc.. getting an overview of the problem might help) – Torxed Mar 28 '14 at 08:38
  • I also tried n = int(input()) but it's about copying and pasting all data to my interpretor(idle for python 3.3.2) – bahadir Mar 28 '14 at 12:56

2 Answers2

0

This looks like a platform issue. It appears your terminal, Python interpreter and paste source disagree on what an end of line is; by the presence of "D:" in the path, I guess it's a Windows platform, where newlines are traditionally stored as '\r\n' and entered as '\r' (carriage return). Python follows the C/Unix tradition of processing newlines as '\n' (line feed). With the wrong combination of terminal handling and clipboard contents, the '\n' character will be passed like any other and not end a line. You can experiment with entering the newline styles manually; carriage return is Ctrl+M and line feed is Ctrl+J. Python also does have support for different newline styles; my guess is that in your particular terminal, sys.stdin is opened with '\r' newlines.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • I use idle for python 3.3.2. So do i have to change settings or some kind of thing? – bahadir Mar 28 '14 at 12:54
  • I imagine it wouldn't be broken if it was already known. To me it sounds like a bug in Idle; it apparently has its own implementations of sys.stdin etc. https://stackoverflow.com/questions/6244956/python-3-2-idle-vs-terminal – Yann Vernier Mar 28 '14 at 19:35
0

int cannot convert \n into int value.

>>> print int('9\n')
9
>>> 
>>> print int('9\n3')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '9\n3'

remove sys.stdin.readline() like following code:

import sys

n = 5 #length
for _ in range(n):
   s = input()
   print(int(s))
user3273866
  • 594
  • 4
  • 8