[Python] How to continue a while loop while using the break function [closed]

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    Hello I am trying to code the following prompt correctly using a while loop:

    "The break statement is used to exit/terminate a loop.

    Design a program that uses a while loop and continuously asks the user to enter a word unless the user enters "chupacabra" as the secret exit word, in which case the message "You've successfully left the loop." should be printed to the screen, and the loop should terminate.

    Don't print any of the words entered by the user. Use the concept of conditional execution and the break statement."

    I am not sure if I am using the break function in the correct position. Also, I get the error:

    SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?


    Here is my code, note I used the str.casefold function so that the user's input is not case sensitive.


    secret_word = "Light"

    while secret_word == "Light":
    print(str.casefold(input("Enter the secret word to exit the black whole: "))):
    if input == secret_word:
    print("You've escaped the black whole, you're safe")
    break
    else:
    if input != secret_word:
    print(str.casefold(input("Enter the secret word to exit the black whole: ")))
    continue

    Continue reading...

Compartilhe esta Página