0

I have been trying to read set of input from prompt. The first line of prompt input represents how many lines will be supplied. For e.g

3 #This represents three values will be supplied in following lines
8
7 
100

no_of_lines = int(raw_input().strip())
lines_content = []
for i in range(0,no_of_lines):
    temp_content = raw_input().strip().split('\n')
    lines_content.append(int(temp_content))

for j in range(0,len(lines_content)):
    print(int(lines_content[j]))

Above is my code in python to read the input. I am expecting that the last print statement would print the contents of type int. But I am not able to do so. Why that type conversion is not happening ?Is there any Pythonic way to achieve above scenario ?

PS Adding Traceback

Traceback (most recent call last):
  File "C:/test_project/test.py", line 5, in <module>
    lines_content.append(int(temp_content))
TypeError: int() argument must be a string or a number, not 'list'
  • It's actually duplicate (from series: Let me google that for you) and answered here: https://stackoverflow.com/questions/30239092/how-to-get-multiline-input-from-user – Drako May 23 '17 at 13:09

1 Answers1

0

You should just loop over until an empty string is matched

no_of_lines = int(raw_input().strip())
nums = [int(raw_input().strip()) for _ in range(no_of_lines)]
for num in nums: 
    print(num)

BTW - your will get an error in your code because split returns a list

 temp_content = raw_input().strip().split('\n') # returns a list

 temp_content = raw_input().strip() # should be enough for your case ... 
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36