拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 我可以在函式中分别运行两个while回圈吗?如果不是,我如何验证python中的串列?

我可以在函式中分别运行两个while回圈吗?如果不是,我如何验证python中的串列?

白鹭 - 2022-03-15 1967 0 0

我可以在函式中分别运行两个while回圈吗?如果不是,我如何验证 python 中的串列?

def ADD_RESULT():

    while True:
        code = input("Enter the code")
        if code in COURSE_CODE :
            print("Details found")
            break
        else:
            print("course  didn't match")
            continue
    print("")

    while True:
        sid = input("Enter student id")
        if sid in STUDENT_ID:
            print(" found")
            continue
        else:
            print(" not found")
            break

如何使用 while 回圈来验证上面的 python 串列?

uj5u.com热心网友回复:

您可以在函式内的回圈之后运行回圈,更好的是您可以在 python 中的回圈之后运行回圈。我看了你的代码,可能有逻辑错误。在 STUDENT_ID 中找到该表达式continue后起作用sid因此回圈再次运行。除非我误解了您想要的内容,否则您应该在第二个回圈中替换continuebreak

while True:
    sid = input("Enter student id")
    if sid in STUDENT_ID:
        print(" found")
        break
    else:
        print(" not found")

此外,您不必continue在回圈结束时使用。因此continue第一个回圈中的陈述句是不必要的。

经过上述修改后,代码如下所示:

def ADD_RESULT():

    while True:
        code = input("Enter the code: ")
        if code in COURSE_CODE:
            print("Details found\n")
            break
        else:
            print("course  didn't match")

    while True:
        sid = input("Enter student id: ")
        if sid in STUDENT_ID:
            print(" found")
            break
        else:
            print(" not found")

uj5u.com热心网友回复:

似乎您在第二个while回圈中的回圈逻辑与第一个回圈中的不同。尝试替换 break 陈述句的位置。此外,此处不需要继续。

def ADD_RESULT():

    while True:
        code = input("Enter the code")
        if code in COURSE_CODE :
            print("Details found")
            break
        else:
            print("course  didn't match")
    print("")

    while True:
        sid = input("Enter student id")
        if sid in STUDENT_ID:
            print(" found")
            break
        else:
            print(" not found")
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *