0

I'm a beginner, and I'm training on CodeAbbey. They give you input, you can hardcode it but I'd rather like to use the input commands.

When it's a single line input it's pretty easy, I read it and store it in a list. This becomes tricky when they give multiple line inputs. At first I cheated a bit, I did copy/paste for each line one after the other. But sometimes there's 40 tests to do, so 40 copy/paste...and I'm lazy. So lazy I spent 2 hours searching how to get my multiple line input in a list.

It's a list of numbers, so basically this is what I do :

list1= [int(x) for x in input("your input").split()]

This works fine to store numbers in a list. But when I have multiple lines in my input, this gives me only the first line. I also tried :

list1= [int(x) for x in input("your input").split("\n")]

Or :

list1= [int(x) for x in input("your input").replace("\n", "")]

These change nothing, only got the first line of input.

I read that the simpler solution would be to store the input a text file and read it with my code, but I did not want to use external files right now as I'm starting to learn.

Thanks for your answers(and excuse my pretty bad english, not a native speaker).

EDIT : The exercise in question : http://www.codeabbey.com/index/task_view/min-of-three

I copy/paste the test data as my input as recommended, but only the first line is stored. I mean, if I print my list, only the first number is displayed, if I print the length of my list with len(), I get 0, which means I have 1 item in my list, right?

TheMontyP
  • 1
  • 1
  • 1
    What indicator do you want that the lines are done? – zondo Mar 08 '16 at 12:52
  • Pro tip is to not use inline-code like you're doing if you're new to this. Split your code into blocks instead. Might make more sense even tho it doesn't solve your actual problem. – Torxed Mar 08 '16 at 12:55
  • Can you link us to the problem on CodeAbbey? You might be going about this in the wrong way – Pep_8_Guardiola Mar 08 '16 at 12:57
  • Note that the linked duplicate refers to `raw_input`, but that function is the same as Python 3's `input`. – chepner Mar 08 '16 at 13:04
  • This is a bit opinionated, but programs that take user input are generally a bad idea. Among other things they are difficult to test and automate. My suggestion is that your starting point should almost always be to take inputs from command line options or from from text files. – FMc Mar 08 '16 at 13:04
  • @FMc `input` doesn't take user input; it reads from standard input, which might be a terminal or a file (and in Unix at least, there's really no difference between the two). It's best thought of as a convenience wrapper that can display an optional prompt, call `sys.stdin.readline`, and strip the trailing newline before returning the input. – chepner Mar 08 '16 at 13:14
  • @GarethWebber The problem on CodeAbbey : http://www.codeabbey.com/index/task_view/min-of-three This is not the only problem with this form, this is why I'm looking for a way to do this which is simpler than copy/paste every line one by one – TheMontyP Mar 08 '16 at 15:48
  • @chepner True enough in the technical sense, but in the context of the OP's question (eg, "40 copy/paste") and in the context of many similar questions and beginner tutorials that encourage this sort of prompt-driven interactivity, there's some logic to the comment. – FMc Mar 08 '16 at 17:00

0 Answers0