Ask coding questions

← Back to all posts
How do i stop a while loop after correct input has been given and continue to run the code after it?
Ardin16 (6)

option_list = ["add", "subtract", "multiply", "divide"]
#^^ list^^
option = input("Do you want to add,subtract, multiply, or divide? Choice: ")
#^^ original input^^
if option not in option_list:
while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
input("Do you want to add,subtract, multiply, or divide? Choice: ")

#^^ while the input does not match an option in option list make the user retry^^
#^^ the while loop does not terminate after the correct input is given and I'm not sure why.
I have tried: else if option not in option list:
break
As well as elif statements, True False statments etc. I'm not sure what else to try.

Answered by RYANTADIPARTHI (5835) [earned 5 cycles]
View Answer
Comments
hotnewtop
RYANTADIPARTHI (5835)

Solution

add a break statement. Once the while loop has succeeded it's task, then add a break.

It should work

Ardin16 (6)

if option not in option_list :
_while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
_input("Do you want to add,subtract, multiply, or divide? Choice: ")
break

like that? _ being a tab
@RYANTADIPARTHI

Ardin16 (6)

when i do that it does break but it does not continue to the part that allows you to run a calculation. it just ends the program, im trying to make it so that once the while loop is broken it will go to the add, subtract, multiply, divide section according to the input that broke the while loop @RYANTADIPARTHI

RYANTADIPARTHI (5835)

@Ardin16 try this code. the if is not necessary.

	while option not in option_list :
		print("Not an option, please use add, subtract, multiply,or divide...")
		input("Do you want to add,subtract, multiply, or divide? Choice: ")
Ardin16 (6)

i did but it doesn't break after i put the right input @RYANTADIPARTHI

RYANTADIPARTHI (5835)

@Ardin16 then in the while loop, make a if statement called if right input, then break.

Ardin16 (6)

@RYANTADIPARTHI what im trying to do is the same as in the code in this repl: https://repl.it/@Ardin16/old-code#main.py
where if you type the user_species wrong, thus it is not in the list, it will make retry but once you get it right it will continue the program.

RYANTADIPARTHI (5835)

@Ardin16 ok, then how about you indent all those if statements into the while loop?

Ardin16 (6)

@RYANTADIPARTHI
it gives me this error when i do it
File "main.py", line 14
if option in option_list :
^
IndentationError: unindent does not match any outer indentation level

, though i might just be messing up the indents, i'll keep trying.

RYANTADIPARTHI (5835)

@Ardin16 ok, keep trying. It's just indentation errors.

Ardin16 (6)

@RYANTADIPARTHI
i finally got it. I had to restate the variable within the while loop so instead of:

if option not in option_list :
while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
input("Do you want to add,subtract, multiply, or divide? Choice: ")

i needed:

if option not in option_list :
while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
option = input("Do you want to add,subtract, multiply, or divide? Choice: ")

Coder100 (15517)

you are adding an unnecessary if statement on line 6 which will always evaluate to false because your loop is trying to fix that, that is why it is not running correctly.
https://repl.it/@Coder100/solution-for-ardin-mark-as-correct-answer-please#main.py

Ardin16 (6)

@Coder100 even if remove it it still continues to say to retry, do i need to put an else statement before the redo input code? The redo code being the print and input inside the while loop.

Ardin16 (6)

@Coder100

i finally got it. I had to restate the variable within the while loop so instead of:

if option not in option_list :
while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
input("Do you want to add,subtract, multiply, or divide? Choice: ")

i needed:

if option not in option_list :
while option not in option_list :
print("Not an option, please use add, subtract, multiply,or divide...")
option = input("Do you want to add,subtract, multiply, or divide? Choice: ")