Python循环从开始而不再运行代码

时间:2017-07-22 05:40:01

标签: python python-3.x

我不知道如何在不重新运行代码的情况下从头开始循环此代码。我知道我应该把True():

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.")

而True:

如果我不想复制代码,我该怎么办?

3 个答案:

答案 0 :(得分:1)

虽然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")

答案 1 :(得分:0)

您是否希望创建一个无限接受搜索参数的程序?

如果是这样,代码将如此:

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.")  

否则请进一步解释问题。

答案 2 :(得分:0)

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: ")

注意:这将持续运行,请确保在您希望代码完成时添加断开条件。