- 1

Can anyone tell me. How loop will run after when a =2 because that would not satisfy the condition. pls explain in brief ??

words = ["hello", "world", "spam", "eggs"] a = 0 b = len(words) - 1 while a <= b: word = words[a] print(word + "!") a = a+ 1

7th Aug 2017, 1:26 PM
Ramesh
Ramesh - avatar
8 Antworten
0
It's completely fine. The discussion board is here to help you. There's no such thing as a stupid question in a help forum, despite what some might try to say to you here. This is an application meant to help beginners get started. You and others are obviously bound to have a question or two. Sometimes even advanced programmers like myself will not always have an answer, and we end up going to look it up for ourselves, study It, then return an answer to you as best as we can. It's a learning tool for both of us. Happy programming!
7th Aug 2017, 2:46 PM
Sapphire
+ 3
The condition is (a<=b) and b is equal to 'words' list length minus one (3, which is the last index of list)... So, the condition is satisfied while a<=3, starting from zero, adding one at each iteration: it will print each items of the list ^^
7th Aug 2017, 1:43 PM
visph
visph - avatar
+ 3
'b' never becomes 1 ^^
7th Aug 2017, 2:25 PM
visph
visph - avatar
+ 2
It doesn't. If you modify the condition inside the while loop, let's say: while a <= b: a += 1 if a == 3: b = 1 print(a) The loop will stop once the final line under it has been executed, the print function for example. The computer will check if the condition was met after it gets to the last line, and since b is less than A, the conditions aren't met since a is larger than b. Therefore the while loop is False. ^^ If b was smaller than a from the start, the while loop will not execute.
7th Aug 2017, 2:32 PM
Sapphire
+ 1
When you say 'while a<=b', the loop will run when 'a' is less than AND equal 'b'. The length of 'b' is the length of words minus 1, which is 3. So the while loop will run 4 times. 0 1 2 3
7th Aug 2017, 1:47 PM
Sapphire
0
my question is how can the loop continues when the value of A becomes 2 and value of B becomes 1. The condition is A smaller than or equal to B but when A becomes 2 and B becomes 1 how can the loop run ??
7th Aug 2017, 2:24 PM
Ramesh
Ramesh - avatar
0
Can you pls explain because I'm getting confused?? As according to the loop B = len(words)-1 and as the value of Increases shouldn't be the value of B decreases ??
7th Aug 2017, 2:28 PM
Ramesh
Ramesh - avatar
- 1
Ohhhh Now I get it... thanks alot guys both of you for your time and patience... I'm new to programming so didn't get it this easily....
7th Aug 2017, 2:41 PM
Ramesh
Ramesh - avatar