Python从一开始就循环,无需再次运行代码
我不知道如何在不再次运行代码的情况下从一开始就循环此代码。我知道我应该说,尽管是真的:Python从一开始就循环,无需再次运行代码,python,python-3.x,Python,Python 3.x,我不知道如何在不再次运行代码的情况下从一开始就循环此代码。我知道我应该说,尽管是真的: list_of_students = ["Michele", "Sara", "Cassie", "Andrew"] name = input("Type name to check: ") if name in list_of_students: print("This student is enrolled.") elif name not in list_of_students: pri
list_of_students = ["Michele", "Sara", "Cassie", "Andrew"]
name = input("Type name to check: ")
if name in list_of_students:
print("This student is enrolled.")
elif name not in list_of_students:
print("This student is not enrolled.")
尽管如此:
如果我不想复制代码,我该怎么办 While True应足以使该输入测试重复运行
while True:
name = input("Enter a name to check: ")
if name in list_of_students:
print("This student is enrolled")
else:
print("This student is not enrolled")
您是否希望创建一个无限接受搜索参数的程序 如果是这样,代码将如下所示:
list_of_students = ["Michele", "Sara", "Cassie", "Andrew"]
while True:
name = input("Type name to check: ")
if name in list_of_students:
print("This student is enrolled.")
else:
print("This student is not enrolled.")
否则请进一步解释问题
list_of_students = ["Michele", "Sara", "Cassie", "Andrew"]
def student_checker(name):
if name == list_of_students:
print("This student is enrolled.")
elif name not in list_of_students:
print("This student is not enrolled.")
# Test to make sure it is working, comment out when not wanted
student_checker("Michele")
student_checker("Anthony")
while True:
name = input("Type name to check: ")
注意:这将持续运行,请确保在希望代码完成时添加中断条件 是的,那是我的问题。我不知道该把这句话放在哪里。谢谢,很好。我很高兴能帮上忙。尽管如此,全面理解代码可能会很有用。你怎么搞糊涂了?感谢@CoryMadden的编辑。没注意到,没问题。只是要更改真实部分,但编辑必须为6个字符或更多。