简体   繁体   中英

How can i use "break" properly into a while loop?

I'm trying to run this program but i get that error massage: 'break' not properly in loop. I've searched for some answers and the reason for the mistake, break cannot be used outside a loop statement.

But as you see bellow, i'm trying to use "break" in a while loop. I'm new to programming, so please don't mind the simplicity of the code.

import random

x = input("Rolar dado? Insira : S/N")

while x == "s":
        print("Nº dado:", random.randrange(1,7))
        x = input("Rolar dado? Insira : S/N")
else:
    break

I expect to shut down the running programm after a user enter "N".

You don´t have to add break in this case, typing N will override the condition of while. Just remove the break statement.

Briefly speaking, the else statement is executed if you exit a block normally, by hitting the loop condition, in your case when x == "s" is False . It is not executed if you break or return out of a block, or raise an exception.

Therefore, there is no point putting a break statement inside an else block of a loop because the break statements are meant to terminate a loop.

"break" should only be used within a while loop. Are you trying to stop execution of the program? if so, use exit()

For example:

import random

x = input("Rolar dado? Insira : S/N")

while x == "s":
        print("Nº dado:", random.randrange(1,7))
        x = input("Rolar dado? Insira : S/N")
else:
    print("Input was not equal to s")
    exit()

The while loop ends when you go out of the indented segment, so since the indentation changed, the else statement isn't in the while statement, which is why it causes the issue.

To get the behaviour you require, you'd want something like

while x == "s":
    print("Nº dado:", random.randrange(1,7))
    x = input("Rolar dado? Insira : S/N")
if x == "n":
    break

While inherently will keep looping as long as the condition is true. As such, in your example, your loop will continue until x == "s", and it will then break out automatically.

#Execute code as long as x is "s"
while x == "s":
        print("Nº dado:", random.randrange(1,7))
        #Get new input for x
        x = input("Rolar dado? Insira : S/N")
        #At this point, we've reached end of while loop, it'll check the condition again
        #If x is still "s", it'll start over at the print line
        #If x is no longer "s" (our condition fails), it stops looping

A break is only necessary if you need an additional exit condition, for example if you just want to wait for up to 3 inputs, and then break,

i = 0
while x == "s":
        i = i + 1
        print("Nº dado:", random.randrange(1,7))
        x = input("Rolar dado? Insira : S/N")
        if i == 3:
            break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM