空列表在while和for循环的上下文中不产生任何输出

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

我写了这段代码来练习循环和嵌套条件。我最初的意图是创建两个空列表,这些空列表将通过附加已排序的输入来逐步填充。但是,当我将列表初始化为at_risk = []safe = []时,没有任何输出。我做错了什么?当我在其中添加一个元素(引号或字符串)时,代码可以正常运行。

i = 10
at_risk = []
safe = []

while i > 0:
    for kids in at_risk:
        question = input("Have you ever been questioned by the police, Y/N? or Q to end: ").upper()
        name = input("What is your name? ")
        age = int(input("How old are you?"))
        if question =="Y":
            at_risk.append(name)
            print("This young kid", name, "is at risk.")
            if age < 15:
                print("These young kids", at_risk, "are at heightened risk.")
        elif question == "N":
            safe.append(name)
            print("This kid",name," is safe.")
            print("These kids",safe,"are safe.")
        else:
            if question == "Q":
                exit(0)
python list loops append
1个回答
1
投票

问题完全在以下代码行内:

at_risk = []
safe = []

while i > 0:
    for kids in at_risk:

您使用针对at_risk中的孩子]初始化循环,但是at_risk为空,因此它将永远不会开始循环。只需更改at_risk,它就可以工作!

© www.soinside.com 2019 - 2024. All rights reserved.