do not understand why this function does not write break will loop infinitely, ask for help
def diedai (size):
size=size+1
print("size=",size)
while (size<=2):
print("size=",size)
diedai(size)
-sharpbreak
diedai (1)
do not understand why this function does not write break will loop infinitely, ask for help
def diedai (size):
size=size+1
print("size=",size)
while (size<=2):
print("size=",size)
diedai(size)
-sharpbreak
diedai (1)
because the size of while is always equal to 2 when it is called for the first time, and it never comes out which is equivalent to
.def diedai(size):
size=size+1
print("size=",size)
while (size<=2):
print("size=",2)
diedai(2)
diedai(1)
diedai (2)
only PP and then print
so it is equivalent to executing this code
size = 2
print("size=", size)
while (size <= 2):
print("size=", size)
print("size=", size + 1)
original code:
def diedai(size):
size = size + 1
print("size=", size)
while (size<=2):
print("size=", size) -sharp a
diedai(size) -sharp b
-sharp break
print("diedai , size=", size)
diedai(1)
The upstairs is quite right. When you first call the detail ()
function, the function enters the while
loop and does not pop up, because the size
you pass in step b only determines whether the next call to the diedai ()
function will enter the while
loop, but will not really change the value of size
. First of all, you should know that this function logic is similar to recursive function
the front part of the execution result
size= 2
size= 2 -sharp ""
size= 3
diedai , size= 3
size= 2
size= 3
diedai , size= 3
size= 2
size= 3
diedai , size= 3
size= 2
size= 3
diedai , size= 3
...
from the first "in loop" to the next "diedai function end", it is a while
loop. The order of execution of this loop is a b a b a. The whole function has been executing these two lines of code.
if you change while
to if
, the function becomes a recursive function, and when the condition is not satisfied, it will jump out of the recursive function layer by layer
because when this program executes to the diedai (2) function. The size in this function is always 2, so it won't stop. The landlord may think that size is changed to 3 when iterating to the diedai (3) function, but in fact the variables in the iterated function have nothing to do with the upper level. That is to say, the size in diedai (3) has nothing to do with size in diedai (2)
size is a local variable, the diedai execution loop in while executes once and ends because of internal size=3, but the external size is always 2, the while loop will not be interrupted, and size will only be interrupted if it is global.
Previous: An example problem of copying constructor
Next: How vue mounts canvas
dict1 = { "system.cpu.user.pct": { "value": 12.83 }, "system.load.1": { "value": 0.33 } } dict2 = { "system": { "...
description: a regular match is given to the content of an input box, and the matching content is the product activation code. looks like this: "0C31-0B81-BB32-3094-0C31-0B81-BB32-3094 " Code: $( -sharplicenseCode ).keyup(function () { le...
for example, the known two-dimensional list [[aformab], [dpene], [f]] requires an element from each sublist to be added and lists all the combinations. The output of this question is adf,aef,bdf,bef,cdf,cef. There are many such lists, and the number of s...
I want the program to randomly issue five cards with different colors and numbers (it doesn t matter if there are occasional cards with the same number), but I find that five cards are always the same. import random suites = [ "Hearts ", "Diamonds ...
this problem works fine after I add code to the first line, but do I have to pay attention to this every time? Python novice asks ...
{% csrf_token%}, is added to the form form of html in the template. MIDDLEWARE = [ django.middleware.security.SecurityMiddleware , django.contrib.sessions.middleware.SessionMiddleware , django.middleware.common.CommonMiddleware , ...
when we use the ansible tool, the default is to use ssh for remote control. you need to generate key pairs in a way similar to ssh-keygen-t rsa , and then use the ssh-copy-id command to distribute the public key file to a remote host and enter the pas...
python replaces the content in front of the vertical bar symbol in the title, but it has been unsuccessful, for example: Chen Ning column (Chinese football fight to correct the name of Wales Uruguay for the championship, replace Chen Ning column for oth...
the article on Wechat s official account crawled through python at the backend found that the video in the article could not be played, and the video was nested in iframe, as shown in <iframe class="video_iframe" data-vidtype="2"...
as in the title, write a simple function test to generate a soup object from the URL using Python requests and BeautifulSoup, (see the example below). If you call this function directly in the main thread, everything will be fine, but if you call this f...
blog s article like function, like once + 1, use session to record the current user problem point is: if you like article 1, article 2 will indicate that it has been supported. The reason is to judge that session how to realize that articles cannot b...
1. Condition: according to whether the alias in the two lines intersect, and if so, Then merge separated by-sharp sign, where: alias 2 in alias separated by-sharp sign, data name alias 0 potato potato-sharp egg-sharp potato-sharp potato 1 potato p...
Baidu said that there is a problem with the verification method in the file var lib pgsql 9.5 data pg_hba.conf, but the reason for changing the verification method is to use the command psql-U postgres-d mydjango-p 5432-h 127.0.0.1 to log in directl...
I ve been looking for it for a long time, but I can t find it. 1. After the direct upload of web is completed, the server cannot know whether the file was uploaded successfully, who uploaded it, and the information of the file 2. Using web to dire...
currently I have a piece of code that spends most of its time on the above two sentences of data filtering in dataframe. temp_df = df [df [ "data_date "] .isin (date_list)] temp = temp_df [rule [2]] [temp_df [ "data_date "] = = d] at present, it tak...
< H2 > 1. Question: < H2 > like the title, how does the chart displayed by matplotlib.pyplot.show () in pycharm pop up to sciview,? < H2 > 2. Code: < H2 > import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib impo...
I need to install both python 2.6 and python 3.6 on my machine. Based on my query, I found that I could use pyenv to do this things, but according to an article I read, python 2 series didn t come with package management tools until python 2.7.9. The...
The question is written on the title, is there any way to be spaced by BAN, when it is applied too fast in crawlers? Or is it that there is no interval in this kind of concurrency? ...
Open a chrome browser using selenium, and open a Baidu web page. Using the send_keys () method to fill in the content in the search box, there are the following problems -sharpcoding:utf-8 from selenium import webdriver from selenium.webdriver.chrom...
the newly installed win7 system, Ann python is, the installation package setup comes out of this, which I have encountered before. I forgot how to solve it. ...