Lesson 009: Amazing branches and cycles 3




Me:5 times

Standard answer: 5 times, because from 0 to 10, the step is 2.

1. How many times will the following cycle print “I love fishc”?


Me: error. The loop should use an array, such as range ()

Standard answer: can report mistakes. We mentioned that in is “membership operator” in the exercises after last lesson, rather than using for syntax like C. Python’s for is more like foreach in the scripting language.

2. Review the role of break and continue in the loop?

Me: Break: terminate loop, jump out of loop

Continue: jump out of the current cycle and enter the next cycle

The function of the break statement is to terminate the current loop and jump out of the loop body.
The function of the continue statement is to terminate the current cycle and start the next cycle (note that the cycle conditions will be tested before the next cycle).



4. What number does range (10) generate?

Me: generate an integer between 0-9

Standard answer: range (0, 10) and list (range (0, 10)) will be generated and converted to list as follows: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Note that 10 is not included.

5. Visually check what the following program will print?


Me:2 and 3

Standard answer:



6. Under what circumstances should we make the cycle true forever?

Standard answer: while ture:
Circulatory body
It is also used for game implementation, because as long as the game is running, it needs to receive user input at all times, so use always true to ensure that the game is “online”. The same is true of the operating system, which is always on standby. The loop that the operating system is always true is called the message loop. In addition, many client / server systems of communication servers work by this principle.
So forever for “true” although it is a “dead cycle”, but not necessarily a bad thing, besides, we can use break to jump out of the cycle at any time!

7. [learn to improve code efficiency] what do you think of the following code efficiency? Is there a way to significantly improve (still using while)?



while i < len_str:
	i += 1

Standard answer: the reason why this code is “inefficient” is that len() needs to be called once per cycle Function (we haven’t learned the concept of function yet. Here’s the explanation of the zero based friend image of Little Turtle: just like when you are playing a game at a high level, my mother asked you to buy salt… You have two options, one bag at a time, five times a day, or five bags at a time. My mother will give it to her directly.)


i = 0
string = 'ILoveFishC.com'
length = len(string)
while i < length:
    i += 1  



use one’s hands




Password = input ("please enter password:")
while i < 3:
	if password.find("*") != -1:
		Password = input ("password cannot contain" * "! You still have "+ str (3-I + 1) +" chances! Please enter the password: ")
		if password == "FishC.com":
			Print ("the password is correct, enter the program...")
	elif password == "FishC.com":
		Print ("the password is correct, enter the program...")
		Password = input ("wrong password! You still have "+ str (3-I) +" chances! Please enter the password: ")
		if password == "FishC.com":
			Print ("the password is correct, enter the program...")
		i += 1


Standard answer:


count = 3
password = 'FishC.com'

while count:
    Passwd = input ('Please enter password: ')
    if passwd == password:
        Print ('correct password, enter program... ')
    elif '*' in passwd:
        Print ('password cannot contain "*"! You still have ', count,' chances! 'end=' '.
        Print ('wrong password! You still have ', count-1,' chances! 'end=' '.    
    count -= 1






for i in range(100,999):
	if i == int(stri[0])**3 + int(stri[1])**3 + int(stri[2])**3:

Standard answer:

for i in range(100, 1000):
    sum = 0
    temp = i
    while temp:
        sum = sum + (temp%10) ** 3
        Temp // = 10 ා pay attention to the use of floor cleaning here~
    if sum == i:





Standard answer:

for red in range(0, 4):
    for yellow in range(0, 4):
        for green in range(2, 7):
            if red + yellow + green == 8:
                #Note that the lower side is not string splicing, so "+" is not needed~
                print(red, '\t', yellow, '\t', green)

Note: range (2, 7) is to generate [2, 3, 4, 5, 6] five numbers, green ball can not be one, because if green ball is one, red ball + yellow ball need to have seven to meet the question, while red ball and yellow ball only have three, so it is range (2, 7)