Small turtle Python eighth lecture exercises

0. How many times will the following loop print "I Love FishC"?
for i in range(0, 10, 2):  
   print('I Love FishC') 
Print 5 times, range(start,stop,interval)
 
1. How many times will the following loop print "I Love FishC"?
for i in 5:  
  print('I Love FishC') 
Error, invalid syntax, 5 is an integer and cannot be used as an iterable value
 
2. Review the role of break and continue in the loop?
Answer: break is to terminate the loop and end the program when the condition is satisfied. Continue terminates the loop and returns to the beginning of the loop body when the condition is true, and executes the statement below continue if the condition is false.
 
3. What numbers does range(10) generate?
Answer: 0,1,2,3,4,5,6,7,8,9 
 
4. Under what circumstances should we make the loop always true?
while Ture:  
   loop body
 
The same is used for game implementations, because the game needs to receive user input at all times as long as it is running, so using always true ensures that the game is "online". The same is true for the operating system. Always on standby, the operating system is always true. This cycle is called the message loop. In addition, many client/server systems of communication servers work on this principle.
     
So always "true" is an "infinite loop", but it's not necessarily a bad thing. Besides, we can use break to break out of the loop at any time!
 
5. [Learn to improve the efficiency of code] What do you think about the efficiency of the following code? Is there a way I can drastically improve this (still using while)?

i = 0  

string = 'ILoveFishC.com'  

while i < len(string)):  

  print(i)   

  i += 1

 

answer:

i =0

string = 'ILoveFishC.com'  

length =len(string)

while i< length:

  print(i)

  i +=1

 

Hands-on:

0. Design a program to verify the user's password. The user has only three chances to enter the wrong password, but if the content entered by the user contains "*", it will not be counted.

When ci==0 written by myself, there is a problem, but it is not solved

mima='123aaa' 
ci=3
temp =input('shurumima:')
while ci:
if temp==mima:
print('zhengque')
break
elif '*' in temp:
temp=input('buenngbaoh *,cishu% s,chongxinshuru'%ci)
elif ci ==0:
print('zhanghaoshuoding,qing10fenzhonghou')
else:
ci -=1
temp=input('cishushaoleyici,%s,chongxinshuru'%ci)

small turtle code:
count = 3
password = 'FishC.com'

while count:
    passwd = input('Please enter the password:')
    if passwd == password:
        print('The password is correct, enter the program...')
        break
    elif '*' in passwd:
        print('Password cannot contain "*"! You still have ', count, 'one chance!', end=' ')
        continue
    else:
        print('Wrong password! You still have ', count-1, 'one chance!', end=' ')    
    count -= 1
 1. Write a program to find the number of all daffodils between 100 and 999.
A 3-digit number is called a daffodil number if it is the sum of the cubes of its digits. For example: 153 = 1^3 + 5^3 + 3^3, so 153 is a daffodil number. 
for a in range(100,1000):
i=a
i==(i//100)**3+((i//10)%10)**3+(i%10)**3
if i==a:
print(i)
else:
print(' ')

小甲鱼代码:
for i in range(100, 1000):
    sum = 0
    temp = i
    while temp:
        sum = sum + (temp%10) ** 3
        temp //= 10 # Note that the floor is used here~
    if sum == i:
        print(i) 

2. The three-color ball problem
has red, yellow, and blue balls, of which there are 3 red balls, 3 yellow balls, and 6 blue balls. First mix the 12 balls into a box, draw 8 balls at random from it, and program to calculate the various color combinations of the drawn balls
Blue must have 2-6 red 0-3 yellow 0-3 

print('red\tyellow\tblue')
for red in range(0, 4):
for yellow in range(0, 4):
for blue in range(2, 7):
if red + yellow + blue == 8:
print(red, '\t', yellow, '\t', blue)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325635114&siteId=291194637