is that in my code I want the user to decide which option they want to see. The problem is that if I put something wrong in "decision" I get an infinite loop of "Wrong Decision". I don't understand why this happens
decision=input("¿Desea ver la matriz de estadisticas modificada o solo la columna calculada? \n Responda 'estadisticas' o 'columna' dependiendo de su decision ")
terminado=False
while terminado==False:
if decision=="estadisticas":
terminado=True
print("La matriz_modificada es {}".format(matriz_modificada))
elif decision=="columna":
terminado=True
print("La columna calculada de autocubrimiento para cada facultad es {}".format(columna))
else:
print("Decision incorrecta")
Thank you very much in advance.
Your program stays in an infinite loop because only at the beginning you ask the user to choose an option, if the user does not choose "column" or "statistics" he does not enter the if or the elif, he goes to the else and in the else he does not know It asks the user what they want again, then it exits the else and returns to the loop without having changed its decision and so on, this is solved by adding this line at the end of the else:
decision=input("¿Desea ver la matriz de estadisticas modificada o solo la columna calculada? \n Responda 'estadisticas' o 'columna' dependiendo de su decision ")
You have an infinite loop because the loop control variable
terminado
is not modified if the el fallselse
, also sincedecision
it is not modified within the loop either, the first two conditionals will never be fulfilled if they did not do so in the first iteration, so once executedwhile
he will never get out of it if decision is not "statistics" or "column".If you want the option to be requested again until something correct is entered, you must request the
input
within thewhile
, thus giving the opportunity for the decision to change, to fall into theif
or theelif
already finished change allowing the cycle to break:If you don't want to ask again, then just the loop is left over:
Although syntactically and functionally correct,
while terminado==False:
(orif algo == True
,algo != True
, etc) is not the correct way to check if something evaluates to true in Python, you should use if/while [not] something:while terminado == False
->while not terminado:
if terminado != False / if terminado == True:
->if terminado:
if terminado == False / if terminado != True:
->if not terminado:
If you want to know if something is the object
bool
True
, which is not the same as knowing if something evaluates to true, you should use the identity operatoris
/is not
, sinceTrue
//False
areNone
singletons: