我调用了一个函数,这样我就可以将名字添加到列表中。除非我输入'End‘或'end’,否则我想继续向列表中添加名称。我使用了一个设置为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()我们非常感谢您的帮助。
发布于 2020-10-27 02:36:42
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 student_name=="End":
break
else:
Student_Names.append(student_name)
names()更改if语句,它起作用了。
https://stackoverflow.com/questions/64542146
复制相似问题