Home > Mobile >  Multiple inputs to multiple loops
Multiple inputs to multiple loops

Time:05-31

I am new to Python (started last week) and I am brainstorming on what is the most efficient way to achieve the following. 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.

CodePudding user response:

Issues

Some syntax-errors fixed and commented below:

  1. if-condition
  2. indents in if-branch
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'}

CodePudding user response:

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:

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'}]
  • Related