[Python] How could I repeat a block of code in scope of a For loop without moving onto the...

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have written a really simple code that functions as a "school register" by asking whether a student is present or not; if something else other than a "yes" or "no" is inputted the code will simply print "invalid input".

    I would like to have a "yes" or "no" answer to each register question by repeating the same For loop without moving onto the next person until the "yes" or "no" has been inputted.


    students = ["Julio","Jayden","Josh","John","Jamal"]

    present = 0
    absent = 0
    for x in students:
    register=input(f"Is {x} present? ")
    if register == "yes":
    present += 1
    elif register == "no":
    absent += 1
    else:
    print("invalid input")
    # ^^^^^^ this is where I would want to make the code repeat the For loop again without moving onto the next person on the register
    print(f"{present} people are present")
    print(f"{absent} people are absent")


    I have tried to use a ' while register != "yes" or "no" ' loop but I don't know how could I make the For loop start all over again

    Continue reading...

Compartilhe esta Página