Great little turtle branch circulation 8.3 summarizes reflection

Summarize the lesson:

for i in range (10):
    if i%2 != 0:
        print (i)
        continue
 #这里continue作用是当if条件成立,则输出i以后直接开始下一次循环,而不会i加2       
    i += 2
    print (i)

5. The following visual program what will print?

while True:
    while True:
        break
        #这个while循环因为break跳出,所以1不打印
        print(1)
    print(2)
    break
print(3)

会打 1
     2 

7. improve the efficiency of the code [learn] how you think the following code efficiency? There is no way you can significantly improve (still use while)?

i = 0
string = 'ILoveFishC.com'
while i < len(string)):
    print(i)
    i += 1
  #这个程序问题是重复使用len()函数,从而提高工作量
  改进::;
  i = 0
string = 'ILoveFishC.com'
length = len(string)
#将string的长度赋值,在比较,这样只会使用一次len()函数
while i < length:
    print(i)
    i += 1  

Programming problem:
0. designed a program to verify the user's password, the user only three chances to make a mistake, but if the user input with "*" are not counted.

number='luo'
count = 3
while count :
#第二次遇到这种,使用while时可以直接while + 变量,然后随着变量递减到0则可以跳出
    key = input('请输入您的密码:')
    #放在这里输入可以使得每次循环开头都直接input,而且每次continue回来都会重新输入,逻辑就非常顺畅
    if number == key:
        print ("恭喜,密码正确")
        break
        #break是跳出本循环外的那个循环
    elif '*' in key:
    #in 做检查作用!且elif是if不成立时继续
        print('密码中不能含有"*"号!您还有',count,'次机会!',end='')
        continue
        #同break,是跳出一级重新继续大循环
    else:
        print ('密码错误,您还有',count-1,'次机会请重新输入!',end='')
        #注意   '密码错误,您还有',count-1,'次机会请重新输入!',end=''中首先是打印字符‘’用引号,然后,count-1,因为count是变量不能加引号,才能得到变量计算的值,然后在引号‘’将剩下的次重新输入写完
    count -= 1     

  1. Write a program, daffodils find all numbers between 100 ~ 999.
    If a 3-digit numbers equal to the cube and you said to this number is the number of daffodils. For example: 153 = 1 ^ 3 ^ 3 + 5 + 3 ^ 3, 153 so that a number daffodils.
for i in range(10,1000):
#range函数运用于一个范围内,和for狼狈为奸,从而一个一个抽取函数
    sum =0
    temp=i
    while temp:
        sum =sum + (temp % 10)**3
        temp //= 10
#设计巧妙,首先利用除10的余数以及while循环(0时结束!第三次遇到了!)
#其次利用浮点计算,从而到十位,百位
    if sum==i:
 #!!!!!一定注意,为什么要用temp = i,是因为要对temp进行处理,从而最后赋值并不是这个,而i是没变的,最后这里所以用i = sum,temp的作用相当于替代i进行一系列处理
        print(i)

2. The three-color ball problems
red, yellow and green colors requirements, wherein three red balls, three balls yellow, green ball 6. This first mixing 12 balls in a box, the ball 8 to work out from any programming work out colors is calculated with the ball.

print('red\tyellow\tgreen')
for red in range(0,4):
    for yellow in range(0,4):
         for green in range(2,7):
  #这里用二到七的原因是绿色至少倆,不然不可能总共八个!
 if red+yellow+green==8:
     print(red,'\t',yellow,'\t',green)
     #!!!和编程题0一样,red是直接输出变量值,而\t是输出一个符号,需要用引号,而这两个值之间需要用逗号隔开!

** summarized: 1.break, continue to terminate or continue a cycle corresponding to the outer loop of the present outside the outside layer
2.while count, which directly can be used as the loop condition, it can be matched to the operating range of 0 thus out of the loop
3. Note that the output of the variable character and mix when print, separated by commas need
4. \ t mean four characters compartment
5. '*' in key among them can check character no yes *
6. the len ( asdasda) represents a character length, preferably chromium can assign additional variables, which can improve coding efficiency
7. the remainder can be used to extract the data on the last bit, floating-point arithmetic may be used with the last one into a ten successive Facing
8 .range and for working hand in glove! Note that combination, corresponding to a variable one extraction can be saved and used to define a loop variable while, etc., to improve efficiency **

Published 17 original articles · won praise 1 · views 364

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105199233