简体   繁体   English

多个循环的多个输入

[英]Multiple inputs to multiple loops

I am new to Python (started last week) and I am brainstorming on what is the most efficient way to achieve the following.我是 Python 新手(上周开始),我正在集思广益,寻找实现以下目标的最有效方法。 I need to create a script that takes multiple inputs of various types from users which then may or may not be added to a loop.我需要创建一个脚本,该脚本从用户那里获取各种类型的多个输入,然后可能会或可能不会将其添加到循环中。 More specifically, I am thinking something along these lines:更具体地说,我在想一些事情:

X =[]
Y =[]
Z =[]
SumofInputs = int(input("Enter # of inputs for field A that will have similar input for field B and C\n"))
for i in range(SumofInputs):
    X.append(input("Provide your input***s*** for field A\n")) 

# I am also not sure here if there is a way that users can provide all their inputs at once and can be seperated by a space?

    Y.append(input("Provide your input for field B\n"))
    Z.append(input("Provide your input for field C\n"))

# From here and below similar inputs from field A that are part of the same "family" need to go to a loop. The rest go to a different loop.

if X === true 
for key in X: 
    print("Yay")
else:
    print("Nope")

# And then the input from the two loops needs to go into the creation of a new dictionary to define an object

new_object = {'name': 'X',
                'city': 'Y',
                'age': 'Z'}

print (new_object)

I am sure that the above has a lot of errors but at least I hope that my goal makes sense.我确信上面有很多错误,但至少我希望我的目标是有意义的。 As said, I started last week so I would appreciate some help or direction on where to focus.如前所述,我是上周开始的,所以我希望能得到一些帮助或指导重点。

To get an array of "words" from a single input string you can write要从单个输入字符串中获取“单词”数组,您可以编写

X = input("Enter inputs for A: ").strip().split()

For example with length checking例如长度检查

X = []
Y = []
Z = []

while(len(X) <= 0):
    X = input("Enter input(s) for A: ").strip().split()
while(len(Y) != len(X)):
    Y = input(f"Enter {len(X)} input(s) for B: ").strip().split() 
while(len(Z) != len(X)):
    Z = input(f"Enter {len(X)} input(s) for C: ").strip().split() 

To access each entry in A, B and C and create a list of dicts with an entry from each:要访问 A、B 和 C 中的每个条目并创建一个字典列表,其中每个条目都有一个条目:

cityList = [{"name": x, "city": y, "age": z} for x, y, z in zip(X, Y, Z)]

Output:输出:

Enter input(s) for A: Morgan Lizzie Trevor Bob
Enter 4 input(s) for B: Arizona Brunswick London Beijing
Enter 4 input(s) for C: 12
Enter 4 input(s) for C: 15 74 35 23

And print(cityList):并打印(城市列表):

[{'name': 'Morgan', 'city': 'Arizona', 'age': '15'}, {'name': 'Lizzie', 'city': 'Brunswick', 'age': '74'}, {'name': 'Trevor', 'city': 'London', 'age': '35'}, {'name': 'Bob', 'city': 'Beijing', 'age': '23'}]

Issues问题

Some syntax-errors fixed and commented below:修复了一些语法错误并在下面进行了评论:

  1. if-condition如果条件
  2. indents in if-branch if 分支中的缩进
X =[]
Y =[]
Z =[]
SumofInputs = int(input("Enter # of inputs for field A that will have similar input for field B and C\n"))
for i in range(SumofInputs):
    X.append(input("Provide your input***s*** for field A\n")) 

# I am also not sure here if there is a way that users can provide all their inputs at once and can be seperated by a space?

    Y.append(input("Provide your input for field B\n"))
    Z.append(input("Provide your input for field C\n"))

# From here and below similar inputs from field A that are part of the same "family" need to go to a loop. The rest go to a different loop.

if X:  # tests on non-empty list instead of  === true 
    for key in X:   # indent below if
        print("Yay")
else:
    print("Nope")

# And then the input from the two loops needs to go into the creation of a new dictionary to define an object

new_object = {'name': 'X',
                'city': 'Y',
                'age': 'Z'}

print (new_object)

Output:输出:

Enter # of inputs for field A that will have similar input for field B and C
2
Provide your input***s*** for field A
hello
Provide your input for field B
world
Provide your input for field C
is
Provide your input***s*** for field A
good
Provide your input for field B
no
Provide your input for field C
is not
Yay
Yay
{'name': 'X', 'city': 'Y', 'age': 'Z'}

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

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