samedi 4 avril 2020

Combine if statement and for loop such that loop only gets executed once per every execution of if statement in swift

I'm trying to understand swift and therefore try to come up with simple command line games: in this game a player has to guess a secret word within 6 attempts by typing something in the command line, but every time he gets it wrong, a statement prints the number of his wrong attempts:

let response = readLine()

if response != "secret word" {
    for n in 1...6 {
        print(n)
    }
}
else {
        print("you are right!")
    }

Now I know that my code will print all lines once the condition is not true, but I'm looking for a way to only print one item out of the four loop for every if statement consecutively.

Aucun commentaire:

Enregistrer un commentaire