While Loop con 2 variables en Python [cerrado]
Frecuentes
Visto 345 veces
-5
Cual es el valor de y
after the following statements?
x = 100
y = 0
while x > 50:
y = y + 1
x = x - 1
I'm having trouble with questions that involve 2 variables.
1 Respuestas
3
Step through the first few iterations of the loop, look for a pattern, and extrapolate.
x = 100 # x = 100
y = 0 # x = 100 y = 0
if x > 50: # x = 100 y = 0
y = y + 1 # x = 100 y = 1
x = x - 1 # x = 99 y = 1
if x > 50: # x = 99 y = 1
y = y + 1 # x = 99 y = 2
x = x - 1 # x = 98 y = 2
if x > 50: # x = 98 y = 2
y = y + 1 # x = 98 y = 3
x = x - 1 # x = 97 y = 3
if x > 50: # x = 97 y = 3
y = y + 1 # x = 97 y = 4
x = x - 1 # x = 96 y = 4
if x > 50: # x = 96 y = 4
y = y + 1 # x = 96 y = 5
x = x - 1 # x = 95 y = 5
if x > 50: # x = 95 y = 5
y = y + 1 # x = 95 y = 6
x = x - 1 # x = 94 y = 6
...
if x > 50: # x = 52 y = 48
y = y + 1 # x = 52 y = 49
x = x - 1 # x = 51 y = 49
if x > 50: # x = 51 y = 49
y = y + 1 # x = 51 y = 50
x = x - 1 # x = 50 y = 50
if x > 50: # x = 50 y = 50
(false, end process)
Respondido el 23 de Septiembre de 13 a las 03:09
Waw! nothing can be better helpful than this answer. - grijesh chauhan
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python while-loop or haz tu propia pregunta.
loop runs for
50
veces, entoncesy
incrementado50
veces por1
. Then what should be the value? - Grijesh ChauhanI don't see that you are taking care of "indentation" which is a must in python. - user2784234
Which part are you stuck on? - John La Rooy
Unless you're taking a pencil-and-paper test, problems like this are best solved empirically:
print(y)
. - FMc