0

I am making a basic program in which I want to store multiple inputs from the user in the predefined list. My approach

exampleList = []
exampleList = input("Enter the choice of user1: ")
exampleList = input("Enter the choice of user2: ")
exampleList = input("Enter the choice of user3: ")
exampleList = input("Enter the choice of user4: ")
exampleList = input("Enter the choice of user5: ")
# I want to store 5 number inputs in examples list

But I don't want to use input function multiple times. Desired Output:

exampleList = [2,3,5,4,1]

2 Answers 2

3

You could store all of these inputs in a list called inputs using the following:

inputs = list()                                                         

for idx in range(1, 5): 
    inputs.append(input(f"Enter the choice of user {idx}: ")) 

Test time using ipython:

In [0]: inputs = list()                                                         
   ...: for idx in range(1, 6):  
   ...:     inputs.append(input(f"Enter the choice of user {idx}: ")) 

Enter the choice of user 1: 12
Enter the choice of user 2: 1234
Enter the choice of user 3: 54326
Enter the choice of user 4: 3232
Enter the choice of user 5: 55 

In [1]: print(inputs)                                                           
['12', '1234', '54326', '3232', '55']
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Sir. I am new to programming so I am learning some basics of it.
My pleasure. If that suits your needs, you may "accept" the answer.
1

You can also try using list Comprehension ,it is most elegant way to define and create a list.

>>>a=[input("enter choice of user%d : "%(i+1)) for i in range(5)]
enter choice of user1 : 2
enter choice of user2 : 3
enter choice of user3 : 4
enter choice of user4 : 5
enter choice of user5 : 1
>>>print(a)
[2,3,4,5,1]

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.