codingbase
codingbase

Reputation: 25

taking multiple inputs into lists

I am trying to take input from a user and store that input in three lists. Currently my code looks like this:

values = int(input())

value = list(map(int, input().split()))
volume = list(map(int, input().split()))
weight = list(map(int, input().split()))

However this requires the user to enter all the value values, then all the volume values, then all the weight values together.

Instead, I would like to have the user enter a value for values and then be prompted values times for values to enter in each of the above lists. I want the user to enter values in triplets of the form value volume weight.

What changes can a I make to my code to achieve my desired result?

Upvotes: 0

Views: 762

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

You can achieve this by using range() to loop the amount of times input under value.

The for loop defines, from 0 --> values and will loop over the code the specified amount of times.

values = int(input('Enter a value: '))

>>>Enter a value: 2

result = []
for i in range(0, values):
    value = input('Enter a Value: ')
    volume = input('Enter the Volume: ')
    weight = input('Enter the Weight: ')
    userinput = [value, volume, weight]
    result.append(userinput)


>>>Enter a Value: 1
>>>Enter the Volume: 500
>>>Enter the Weight: 1500
>>>Enter a Value: 2
>>>Enter the Volume: 456
>>>Enter the Weight: 1789

result
[['1', '500', '1500'], ['2', '456', '1789']]

More reading on Range().

Upvotes: 1

Related Questions