Home > Mobile >  Can I run two while loop separately in function? if not how do I verify the list in python?
Can I run two while loop separately in function? if not how do I verify the list in python?

Time:01-10

Can I run two while loop separately in function? if not how do I verify the list in 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

How do I use the while loop to verify this above python list?

CodePudding user response:

You can run a loop after a loop inside a function, even better you can run a loop after a loop in python. I read your code, there is probably a logic error. The expression continue works after sid is found in STUDENT_ID. Therefore the loop runs again. Unless I'm misunderstanding what you want, you should replace continue with break inside the second loop.

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

Also you don't have to use continue at the end of the loop. Therefore continue statement in the first loop is unnecessary.

After above revisions, code seems like below:

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

CodePudding user response:

It seems like your loop logic in the 2nd while loop is not the same as in the 1st. Try replacing the position of the break statement. Also, continue is not necessary here.

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")
  •  Tags:  
  • Related