-2

I have this code, what I want it to do is take a user input of something like:

1

2

3

4

5

And turn it into: 1, 2, 3, 4, 5

My code doesn't seem to do it, it just returns the first word in the list, any help would be amazing.


i = raw_input("Put the list here: ")
print (i.replace("\r", ", "))

I changed the format, it will now pull the input from a text file, this seems to work as long as each line is only seperated by a single line break

i = open("input.txt", "r")
o = open("output.txt", "w")


for line in i.readlines():
    o.write (line.replace("\n", ", "))

i.close()
o.close()
Tom
  • 3
  • 3
  • 1
    i dont see the link between your code and what you are asking.... you ask for user input, and finally you print the input in your console once? where is the list you mention? – glls May 31 '16 at 02:50
  • This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please [edit] your question and clearly describe what exactly you're trying to do, and where exactly you're stuck. All the answers below are just guesses based on what they *think* your question is, when in reality no one knows. – MattDMo May 31 '16 at 03:43
  • So are you trying to replace multiline string input with a comma-separated string of the values? There's no list here at all... – MattDMo May 31 '16 at 03:45
  • Possible duplicate of [Possible to get user input without inserting a new line?](http://stackoverflow.com/questions/7173850/possible-to-get-user-input-without-inserting-a-new-line) – Serenity May 31 '16 at 03:52

3 Answers3

1

When the user press enter after the 1 raw_input stops reading.

Take a look at this question to see how to read multiple lines

Raw input across multiple lines in Python

Community
  • 1
  • 1
Juanín
  • 841
  • 6
  • 16
1

You can write a simple loop and break with some specified command. For example

break_command = 'q'
values = []
while True:
    i = raw_input("Type a number (or q to exit): ")
    if (i==break_command): break
    values.append(i)
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • What about if I pulled the user input from a text file? That seems like it would be easier – Tom May 31 '16 at 03:25
1

If you want to take multiple user inputs, put all the inputs into a list, and then print out the list, try this:

listX = []                 #An empty list
for x in range(y):         #Where y in the number of times you want the user to enter the input
    listX.append(raw_input("Your message here: "))
print listX

If you want to to print out the members of the list without the square brackets, you could convert the list into a string:

listX = str(listX)

Following this, since the square brackets constitute the first and last characters of the string only, implement the following command:

listX = listX[1:-1]

Of course, it is possible to implement the previous two commands simultaneously:

listX = str(listX)[1:-1]

However, note that the final value of listX is of type string. If you require this value as in int, replacing the line:

listX.append(raw_input("Your message here: "))

with:

listX.append(int(raw_input("Your message here: ")))

and simply printing out the list may be a more efficient, if not better, option.

Chamika Fonseka
  • 29
  • 1
  • 1
  • 7