While loop statement
grammar
while condition:
loop body
Endless loop
The loop that will not exit, the infinite loop will take up a lot of CPU time and keep the program stuck there, but in some programming, the infinite loop is an essential feature, such as server and game development
For loop
Python's for loop can automatically call the next method of the iterator, it will automatically catch the stopinteraction exception and end the loop
grammar
for target in expression: # target is the variable of each iteration, the expression is a list or a tuple
loop body
range() function
range() is a BIF function, it can generate a number sequence (iterable object) for the specified integer, it is a small partner of the for loop
grammar
- range([start,]stop[,step=1])
- This BIF has three parameters, the two enclosed in square brackets indicate that these two parameters are optional, but the parameters must be integers
- step=1 means that the default value of the third parameter is 1
- The function of the range() BIF is to generate a sequence of numbers starting from the value of the start parameter to the value of the stop parameter.
One parameter range()
list()
The number sequence generated by displaying the iterable object in the form of a list only contains the starting value, not the ending value
range() and the mess of the for loop:
Two parameter range()
Range() with three parameters
break statement
effect
Terminate the current loop, jump out of the loop body
right = 'pdd好帅哦'
answer = input('请输入对pdd的描述:')
while 1:
if answer == right:
break
answer = input('打错了哦,要输入正确才可以退出游戏哦:')
print('是的呢,帅是他的第一特征哦')
print('退下吧')
continue statement
Features
Terminate the current cycle and start the next cycle. Note that before starting the next cycle, the cycle conditions will be tested first. Only when the cycle condition is True will the next cycle start, otherwise exit the cycle
for i in range(10):
if i % 2 != 0:
print(i)
continue#如果i是奇数就不执行下面的语句了,再次从for循环开始
i += 2
print(i)#所以最后打印的结果是:0到9所有奇数,和偶数加2
TASK
- How many times will the following loop print "I Love FishC"?
for i in range(0, 10, 2):
print('I Love FishC')
5 times because i has 5
- How many times will the following loop print "I Love FishC"?
for i in 5:
print('I Love FishC')
Will report an error
-
Recall the role of break and continue in the loop?
break: terminate the current loop, jump out of the loop body
continue: jump out of this round of loop and start the next round of loop -
Please talk about your understanding of the list?
List is to display the iteration objects one by one in the form of a list -
What numbers does range(10) generate?
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 -
What will the following program print visually?
while True:
while True:
break
print(1)
print(2)
break
print(3)
Print 2 and 3 break can only jump out of a loop
6. Under what circumstances should we make the loop always true?
while True: The
loop body 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 for the operating system. It is always on standby and the operating system is always true. This cycle is called the message cycle. In addition, many client/server systems of communication servers also work through this principle.
7. [Learn to improve code efficiency] What do you think of the following code efficiency? Is there a way to improve it drastically (still using while)?
i = 0
string = 'ILoveFishC.com'
while i < len(string):
print(i)
i += 1
The reason why this code is "inefficient" is that the len() function needs to be called once every time it loops. After improvement:
i = 0
string = 'ILoveFishC.com'
length = len(string)
while i < length:
print(i)
i += 1
9. Design a program to verify the user password, the user only has three chances to make a mistake, but if the user input contains "*", it will not be counted. (Well! I didn't write the perfect code, copy it from the turtle)
The program demonstration is shown in the figure:
count = 3
password = 'FishC.com'
while count:
passwd = input('请输入密码:')
if passwd == password:
print('密码正确,进入程序......')
break
elif '*' in passwd:
print('密码中不能含有"*"号!您还有', count, '次机会!', end=' ')
continue
else:
print('密码输入错误!您还有', count-1, '次机会!', end=' ')
count -= 1
-
Write a program to find the number of all daffodils between 100 and 999.
If a three-digit number is equal to the cube sum of its digits, then this number is called the daffodil number. For example: 153 = 1^3 +5^3 + 3^3, so 153 is a number of daffodils.
My code
for i in range(100,1000):
a = int(i / 100)
b = int((i - a * 100) / 10)
c = i -(a * 100 + b * 10)
if i == a ** 3 + b ** 3 + c ** 3:
print(i)
Little turtle code:
for i in range(100, 1000):
sum = 0
temp = i
while temp:
sum = sum + (temp%10) ** 3
temp //= 10 # 注意这里要使用地板除哦~
if sum == i:
print(i)
- Three-color ball problem
There are three colors of red, yellow, and blue balls, including 3 red balls, 3 yellow balls, and 6 green balls. First mix the 12 balls in a box, draw 8 balls from it, and program to calculate the various color combinations of the balls.
for green in range(1,7):
for red in range(0,4):
for yellow in range(0,4):
num = green + red + yellow
if num == 8:
print(green,red,yellow)