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
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:
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
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
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!
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?