简体   繁体   English

将多个输入放入列表

[英]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.然而,这需要用户输入的所有value的值,则所有的volume值,则所有的weight值加在一起。

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.相反,我想让用户输入值的values ,然后提示values时间在上述每个列表中输入值。 I want the user to enter values in triplets of the form value volume weight .我希望用户以value volume weight形式的三元组输入值。

What changes can a I make to my code to achieve my desired result?我可以对代码进行哪些更改以达到我想要的结果?

You can achieve this by using range() to loop the amount of times input under value.您可以通过使用 range() 循环输入低于 value 的次数来实现此目的。

The for loop defines, from 0 --> values and will loop over the code the specified amount of times. for 循环定义从 0 --> 值,并将循环代码指定的次数。

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() .更多阅读Range()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM