How do i stop a while loop after correct input has been given and continue to run the code after it?
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.
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
@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.
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: ")
Solution
add a
break
statement. Once the while loop has succeeded it's task, then add abreak
.It should work
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 hmm, yes i think so.
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
@Ardin16 try this code. the
if
is not necessary.i did but it doesn't break after i put the right input @RYANTADIPARTHI
@Ardin16 then in the while loop, make a if statement called if right input, then break.
@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.
@Ardin16 ok, then how about you indent all those if statements into the while loop?
@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.
@Ardin16 ok, keep trying. It's just indentation errors.
@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: ")
@Ardin16 great!