0

Here what I want is to accept user inputs they could be

Hello I am Ram
I am from pokhara
I need to go home

And my list should be:

a[[Hello I am Ram],[I am from pokhara],[I need to go home]]

What I tied is

a = []
for x in range(4):
    a[i] = ([let for let in input('')])

as in How to accept user input in multi dimension list in Python?

9
  • Do you really want a list of single element lists as opposed to a list of N elements? Commented Aug 19, 2017 at 9:34
  • a list should have 3 list each internal list should accept one line and other to be go to next internal list. Commented Aug 19, 2017 at 9:37
  • the input is given with all 3 lines, or each line separately ? Commented Aug 19, 2017 at 9:37
  • There are 3 lines each separated with Enter. Commented Aug 19, 2017 at 9:38
  • 2
    To put it another way... does: a = [input('Enter line {}'.format(n)) for n in range(1, 4)] do what you want? Commented Aug 19, 2017 at 9:41

3 Answers 3

0

Use python3 command to run.

a = []
for x in range(4):
    a.append([input('')])
print (a)

Length:

a = []
for x in range(4):
    l = input('')
    a.append(l)
    print (len(l))

repetittion:

d = {}
for n in range(1, 4):
    v = input('Enter line {}'.format(n))
    if v in d:
        d[v] = d[v] + 1
    else:
        d[v] = 1
print (d)
Sign up to request clarification or add additional context in comments.

4 Comments

just append the input without processing is more like what is expected
as @PRMoureu says...looks like the op just wants to add the line, not a list of the individual characters...
How to count the length of the internal string?
Or you want count repetition?
0

Your input seems to be:

>>> input_string = 'Hello I am Ram\nI am from pokhara\nI need to go home'

Hence you can you can use list comprehension to get your result:

>>> a = [[s] for s in input_string.split('\n')]
>>> print(a)
    [['Hello I am Ram'], ['I am from pokhara'], ['I need to go home']]

Comments

0
input_list = []

while(1):

    u_input = input()

    if not u_input:

        break

    input_list.append([u_input])

print input_list

This will accept input unless you dont give an type anything and give enter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.