【发布时间】:2023-03-09 13:18:01
【问题描述】:
我有一个我调用的函数,因此我可以将名称添加到列表中。除非我输入“结束”或“结束”,否则我想继续将名称添加到列表中。我正在使用设置为 True 的 While 循环来执行此操作,并使用 if/else 语句来附加列表。但是,当我运行我的代码时,它只在离开之前运行一次 While 循环。我有一个类似的函数做同样的事情,除了用数字而不是名字,这没问题。 我之前使用过它,它没有任何问题,但我看不出发生了什么改变以使 While 循环不起作用。
Student_Names=[]
def names():
student_name=str(input("Please enter the students name and type when complete: "))
Student_Names.append(student_name)
while True:
student_name=str(input("Please enter the students name and type when complete: "))
if student_name ==('end') or ('End'):
break
else:
Student_Names.append(student_name)
names()
非常感谢任何帮助
【问题讨论】:
标签: python loops while-loop