- 1

Need solution

What is the output? x=0 while x<5: x=x+2 print(x) How the answere is 6? Prblem:2 x=0 a=[ ] while x <=3: x*=2 a.append(x) print(a) How the answere is infinite loop? Explain please!

27th Nov 2022, 3:52 AM
Mustakim Rahman
Mustakim Rahman - avatar
11 Réponses
+ 4
Problem 1 X= 0, in first loop x= x+2 = 2, second 2+2;= 4, and third 4+2= 6, then x>5 so while loop breaks. And x is printed. Problem 2 Your while loop is an infinite loop because perhaps you forgot to increase x, in the loop.
27th Nov 2022, 4:28 AM
Lucifer
+ 2
No, you are misunderstanding while loop with for loop perhaps or idk. It is straight x= 0+2, 2+2,4+2
27th Nov 2022, 4:45 AM
Lucifer
+ 2
Thanks Mozzy Lucifer for helping 🖤
27th Nov 2022, 4:50 AM
Mustakim Rahman
Mustakim Rahman - avatar
+ 1
Problem 1: The while loop does 3 iterations before exiting, each time adding 2 to x. So 2 + 2 + 2 = 6. Right before the 4th iteration, x < 5 is no longer true, so the loop exits. Problem 2: It is an infinite loop because x is always 0, which means x <= 3 is always true. This is because x *= 2 computes 0 * 2 = 0.
27th Nov 2022, 4:28 AM
Mozzy
Mozzy - avatar
+ 1
But while x<5 mean x=0,1,2,3,4 Why just 2+2+2?
27th Nov 2022, 4:39 AM
Mustakim Rahman
Mustakim Rahman - avatar
+ 1
While loop does not increase the x automatically, it is increasing by 2 because it is in the code 'x = x+2'
27th Nov 2022, 4:42 AM
Lucifer
+ 1
x increases by 2 each time because of `x = x + 2`. So the x values are not 0, 1, 2, 3.. but 0, 2, 4, 6
27th Nov 2022, 4:42 AM
Mozzy
Mozzy - avatar
+ 1
prloblem 1 If i share what i think about that is X=0+2, x=2+2 X=1+2, x=3+2 X=2+3, x=5+2 Like that???
27th Nov 2022, 4:43 AM
Mustakim Rahman
Mustakim Rahman - avatar
0
For problem 1, X = 4+2 = 6 ( because our condition is x<5 so, lastly we have to consider x = 6 as our final answer.
27th Nov 2022, 11:14 AM
Anonymous
Anonymous - avatar
0
Hello can anyone please tell me how python helpful?
27th Nov 2022, 11:14 PM
Bushra Saleem
Bushra Saleem - avatar
0
Every programming language is important. A decades ago java was the most popular programming language. After that python took place of java. Now python is learning rapidly. You-tube platform is also made by several type of programming languages. Python is one of them. Python's syntex is much easier than java. And why you telling what is the important of python! or how python helpful! Just simply search on google or youtube to know about python. Good luck Bushra Saleem
28th Nov 2022, 4:15 AM
Mustakim Rahman
Mustakim Rahman - avatar