我正在编写一个程序,该程序不断要求用户输入名称,直到输入单词END, 这时它会打印出名称列表。循环并追加名称列表
代码:
import getpass
import time
import sys
print("Welcome " + getpass.getuser() + "...")
time.sleep(0.25)
print("This program, powered by Python, it will ask you to enter names...")
time.sleep(0.5)
print("...once you have finished, enter END to print off your list")
names = []
for i in names:
name = input("Please enter a name: ")
if name == "END":
print(names)
sys.exit()
names.append(name)
的问题是,在程序退出之前就尝试为循环做。
为什么会发生这种情况,应该如何解决?