我如何在列表中的特定位置使用 while 来使用嵌套循环?

问题描述 投票:0回答:1

所以基本上我想问用户跟踪费用中有多少项目以及他将给予的项目数量

我会一直告诉他他的价格是多少

necessity=["food","rent","transportation"]
questions = [
    ["how many items", "how much is the price of each item"],
    ["how much is the rent"],
    ["how much is the transportation"]
]
answers=[]

for i in range(len(necessity)):
    print(f"Necessity: {necessity[i]}")
    sub_answers = []
    for question in questions[i]:
        answer = input(question)
        sub_answers.append(answer)
    answers.append(sub_answers)

for j in enumerate (sub_answers[2]):
    print(j)
print(answers)
python list nested-loops
1个回答
0
投票

我认为你想做的是

zip
necessities
questions
,以便你可以迭代与每个必要性相关的问题:

necessity=["food","rent","transportation"]
questions = [
    ["how many items", "how much is the price of each item"],
    ["how much is the rent"],
    ["how much is the transportation"]
]
answers=[]

for n, q in zip(necessity, questions):
    print(f"Necessity: {n}")
    sub_answers = [input(sub_question) for sub_question in q]
    answers.append(sub_answers)

print(answers)
Necessity: food
how many items3
how much is the price of each item2
Necessity: rent
how much is the rent1000
Necessity: transportation
how much is the transportation50
[['3', '2'], ['1000'], ['50']]
© www.soinside.com 2019 - 2024. All rights reserved.