-2

I am wondering:

First, is it possible to exit a while True loop and move onto the next piece of code? This is my while True code that I am using:

while True:
  b = input("Which row of transition metals would you like to find out the melting and boiling points of? ")
  print("")
  if b == "1" or b == "1st" or b == "first" or b == "First":
    print("First row of transition metals:")
    slg(listA, a)
    again = input("Continue? ")
    if again == "yes":
      continue
  elif b == "2" or b == "2nd" or b == "second" or b == "Second":
    print("Second row of transition metals:")
    slg(listB, a)
    again = input("Continue? ")
    if again == "yes":
      continue

Is it possible so that if I had if again == no, it would move on to the next piece of code, exiting the while True loop?

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
DasGuyDatSucks
  • 103
  • 1
  • 11

4 Answers4

2

Use break statement to exit out of the while loop. It works with for loop as well.

eMad
  • 998
  • 3
  • 13
  • 31
1

The break keyword is used to exit a loop. Note that you don't need to repeat code that asks for continuation; you can write it once after your if statement.

while True:
    b = input("Which row of transition metals would you like to find out the melting and boiling points of? ")
    print("")

    if b == "1" or b == "1st" or b == "first" or b == "First":
        print("First row of transition metals:")
        slg(listA, a)
    elif b == "2" or b == "2nd" or b == "second" or b == "Second":
        print("Second row of transition metals:")
        slg(listB, a)
    else:
        # Without a valid response, ask again
        continue

    # Only ask to continue or break after a valid response
    again = input("Continue? ")
    if again != "yes":
      break
chepner
  • 497,756
  • 71
  • 530
  • 681
0

you can also use approach like this, where you set the conditional variable true at begining then in code when the condition to set code exit come make that variable False

Below is a example code

 cond = True
 while cond:
        // some code
        if <condition>:
                continue
        else:
               cond = False
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0
count =1

while True:
    print("Inside while loop")
    for i in "python":
        if i == "h":
            count = 5
            break
        else:
            print(i)
    if count == 5:
        break

Please have a look at the above code. The output for the above code is -

Inside while loop
p
y
t

The above code shows that the keyword break can be used to exit from for and while loops. The keyword continue takes it to the to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

From what you asked, if again == no, if this is provided you can exit from the while loop if you give something like this

if again == no:
     break

But if you just give the below code,

if again == no:
    continue

it will still be in the while loop.

Hope this helps.

Stan11
  • 274
  • 3
  • 11