How to stop the while loop from functioning

Question:
how can I stop the while loop from functioning if the user enters the right input?

Current behavior:

Desired behavior

Repl link:

code snippet

Please indicate which programming language you are using.

You can use several ways for Python:

  1. Using an infinite loop and the break function.
while True:  # infinite loop
  if data_is_right == True:  # put your condition here
    break  # exit the loop
...  # the code that will be executed after receiving the correct data
  1. Using a condition inside the definition of the while loop.
data_is_right = False
while data_is_right == False:  # put your condition here
  ...  # defining the data_is_right variable
...  # the code that will be executed after receiving the correct data
1 Like

Hi @23balijat , welcome to the forums!
Please indicate what language this is in.
Example:
Python:

name=input('Enter your name:')
while name!='N': # Will execute the code below if the name is not N
  name=input('Enter your name:')

JavaScript:

let name=prompt('Enter your name:');
while (name!='N') { // Will execute the code below if the name is not N
  let name=prompt('Enter your name:');
}

Hope this helps!

1 Like

no need for == True and instead of == False you can use not data_is_right


that’s… that’s just what Alex said

that… js code… doesn’t even work, no parentheses around condition

let name;
while (name !== 'N') {
  name = prompt('Enter your name:');
}

but name will always be N here (and in the Python example) so what’s the point?

2 Likes