On 2022-08-05 9:34 AM, ojomooluwatolami...@gmail.com wrote:
Hello, I’m new to learning python and I stumbled upon a question nested loops. 
This is the question below. Can you please how they arrived at 9 as the answer. 
Thanks

var = 0
for i in range(3):
   for j in range(-2,-7,-2):
     var += 1
      print(var)


Welcome to Python. I am sure you are going to enjoy it.

To learn Python, you must learn to use the Python interactive prompt (also known as the REPL).

Type 'python' at your console, and it should bring up something like this -

C:\Users\E7280>python
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

The confusing part of your example above is 'for j in range(-2,-7,-2)'.

To find out what it does, enter it at the '>>>' prompt -

>>> for j in range(-2, -7, -2):
...     print(j)
...
-2
-4
-6
>>>

For the purposes of your exercise, all you need to know at this stage is that it loops three times.

Does that help answer your question? If not, feel free to come back with more questions.

BTW, there is an indentation error in your original post - line 5 should line up with line 4. It is preferable to copy/paste your code into any messages posted here rather than type it in, as that avoids the possibility of any typos.

Frank Millman

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to