我写了这段代码来练习循环和嵌套条件。我最初的意图是创建两个空列表,这些空列表将通过附加已排序的输入来逐步填充。但是,当我将列表初始化为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)
问题完全在以下代码行内:
at_risk = []
safe = []
while i > 0:
for kids in at_risk:
您使用针对at_risk中的孩子]初始化循环,但是at_risk为空,因此它将永远不会开始循环。只需更改at_risk,它就可以工作!