0
Is her anyone who can help me with a for while loop?
a = int(input("insert a number:")) z = 2 for i in range (1, a, 2): z = z * i while (z < 5): z = z + 1 print(str(z)) if i insert number 5 after the request, what will be my Solution and how does the loop work? many thanks in advance:)
2 Respuestas
+ 1
you insert 5
==> a = 5
z= 2 (initially)
i = 1 ( for loop starts from here)
z = z*i => 2*1 => 2
as z < 5 (body ofwhile loop starts executing)
z = z +1=> 2+1 = 3
# its still less than 5
# while loops doesn't end till z= 5
z = z+1 (executes again)
.
now as z = 5 while loop will terminate
# we are back to our for loop,
# range has a skip-factor of 2
# so i= 1+2 = 3
z= z*i => 5*3 => 15
# as z<5 is false while loop will not execute,
not even once
# again i = 3+2= 5
# but as a= 5 range only return [1,3] (not 5)
hence for loop also terminates here.
finally
print (str(z)) => 15
Edit: go through these lessons 👇🏻
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2281/
https://www.sololearn.com/learn/Python/2435/
0
thaaanks man you saved me! i understand it much better than before❤❤❤❤