Python - Dumb ques about nest loop in Python
NickName:J.Zeng Ask DateTime:2017-08-07T13:43:15

Python - Dumb ques about nest loop in Python

I got a dumb question about nest loop in Python.

I just want to figure out why that second statement will follow the first statement to loop 5 times? Below is my loop code, hope someone can explain it to me. Thanks in advance! :)

for steps1 in range(5):
   print('@@@')
   print('@@@')
   for steps2 in range(4):
          print('###')
          print('###')

Copyright Notice:Content Author:「J.Zeng」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45540085/python-dumb-ques-about-nest-loop-in-python

Answers
Claudio Brasser 2017-08-07T05:50:23

This code should execute as follows: For each cycle of the outer loop, the inner loop executes 4 times. (If you struggle with that a pen & paper run simulation might be helpful). That being said your output should look somewhat like that:\n\n@@@ //First outer cycle\n@@@\n### //First inner cycle\n###\n### //Second inner cycle\n###\n### //Third inner cycle\n###\n### //Fourth inner cycle\n###\n@@@ //Second outer cycle\n@@@\n...\n\n\nThis is how nested loops work in most languages. Since Python does not use brackets to determine function scopes you have to give great attention to intendation. If you would like your second for statement to run only 4 times, you would have to write your code like this:\n\nfor steps1 in range(5):\n print('@@@')\n print('@@@')\nfor steps2 in range(4):\n print('###')\n print('###')\n\n\nEdit: More clarification",


More about “Python - Dumb ques about nest loop in Python” related questions

Python - Dumb ques about nest loop in Python

I got a dumb question about nest loop in Python. I just want to figure out why that second statement will follow the first statement to loop 5 times? Below is my loop code, hope someone can explai...

Show Detail

python 3.7 while true loop gives error about unindent does not match any outer indentation level

user = "admin" password = "admin" while true: ques1 = input("Enter your username: ") ques2 = input("Enter your password:") if ques1 == user and ques2 == password : print("W

Show Detail

How to install nest in Python3 on Ubuntu 18.04

After following the Ubuntu/Debian installation instructions for the Nest simulator I can only import the nest module in python2.x, not python3.x $ python3 Python 3.6.8 (default, Aug 20 2019, 17:12...

Show Detail

Can you run two infinite loops, one with an input, at the same time in python?

I've seen some posts about running infinite loops in python but nothing covering this specific topic. I was wondering if you could run code that asked the user a question for an input, whilst another

Show Detail

Iterate by two using a for loop in Python

I'm sorry for something that is a very dumb question. I'm a C programmer so I'm not that used to Python. How would you write the equivalent for loop into python: int x; for(x = 0; x < 10; x+=...

Show Detail

Nest API - APIError: blocked - Using Python Package

I'm using the Nest API to poll the current temperature and related temperature data from two of my Nests (simultaneously). I was initially polling this data every minute but started getting an er...

Show Detail

Can the nest cam api calls be made using python?

My task is to integrate the Nest Cam with a python code. When nest Cam detects a motion , it should then take a picture and send it to my python code for further processing. How this task can be ac...

Show Detail

Nest simulator: python says “no module named nest”

After installing the Nest Neural Simulator, I keep getting the following error when trying to run any of the example python files that came in the installation. I've tried re-installing Nest, Pytho...

Show Detail

NEST simulator:python says "ModuleNotFoundError: No module named 'nest'"

My python version in mac is 3.8 but I need 3.7 so I create environment python 3.7, and install the nest with conda. But when I import nest, there is still the error:ModuleNotFoundError: No module n...

Show Detail

Why python2.7 import "from tensorflow.python.util import nest " failed

i used python2.7 is failed in python2.7 >>> from tensorflow.python.util import nest Traceback (most recent calllast): File "<stdin>", line 1, in <module> ImportE

Show Detail