007, 008 Great branches and loops 1, 2
1. What does assert do?
assert is a keyword we call assertion. When the condition behind this keyword is false, the program automatically crashes and throws an AssertionError exception.
In what circumstances do we need such code? It works well when we test a program because instead of letting the wrong conditions cause the program to crash unexpectedly in the future, we can "explode" at the moment the wrong conditions occur.
The assert keyword is useful when you need to make sure that a condition in your program must be true in order for the program to work properly.
2. Assuming that there is x = 1, y = 2, z = 3, how can you quickly exchange the values of the three variables?
x, y, z = z, y, x
3. Guess what (x < y and [x] or [y]) [0] does?
This is the fact that before Python's author added a ternary operator to Python, the Python community's small partners had the flexibility to use ands and ors to implement the functionality of the ternary operator, with knowledge about lists and slices, which will be explained soon and can be previewed by eager friends.
Python's authors have been reluctant to join a ternary operator for a long time, fearing an international chaos contest like C. Beginners are daunted by the complexity of the pain. However, once you know how to use the ternary operator, some of the more complex problems may be solved.
Modify the following code to a ternary operator implementation:
x, y, z = 6, 5, 4 if x < y: small = x if z < small: small = z elif y < z: small = y else: small = z
small = x if (x < y and x < z) else (y if y < z else z)
Speech 009: Great Branches and Cycles 3
Test questions:
7. [Learn to Improve Code Efficiency] How do you feel about the following code efficiency areas? Is there any way to significantly improve (still use while)?
i = 0 string = 'ILoveFishC.com' while i < len(string): print(i) i += 1
Do it:
0. Design a user password validation program. The user has only three chances to enter an error, but does not count if'*'is included in the user's input.
The program is illustrated in Fig.
[Write it yourself!]
star = ('*') code=('0601') i = 2 temp = input('Please input a password:') while star in temp: print('Password cannot contain"*"No. 3 more chances for you!') temp = input('Please input a password:') continue else: if temp == code: print('Password correct, enter program...') else: print('Wrong password entry!') temp = input('You have 2 more input opportunities:') if temp == code: print('Password correct, enter program...') else: print('Wrong password entry!') temp = input('You have one more entry opportunity:') if temp == code: print('Password correct, enter program...') else: print('Wrong password entry! Please try again in 5 minutes')
[Reference Answer]
count = 3 password = 'FishC.com' while count: passwd = input('Please input a password:') if passwd == password: print('Password correct, enter program......') break elif '*' in passwd: print('Password cannot contain"*"Number! You still have', count, 'Second chance!', end=' ') continue else: print('Wrong password entry! You still have', count-1, 'Second chance!', end=' ') count -= 1
[Write it again]
count = 3 code = '0601' while count: cd = input('Please input a password:') if cd == code: print('Password correct, enter program...') break #Must write, if not, always'Enter password:' elif '*' in cd: print('Password cannot contain"*"Number! You still have ', count, ' Second chance!') continue #I didn't add it, that's okay. But if [count-= 1] tightens forward, it needs to be added, otherwise count will stay at -1 until 0. else: print('Wrong password entry! You still have ', count-1 , ' Second chance!') count -= 1
1. Write a program to count all daffodils between 100 and 999.
If a three digit number equals the cube of its digits and the number of daffodils, call it the number of daffodils. For example, 153 = 1^3 + 5^3 + 3^3, so 153 is the number of daffodils.
[Write it yourself!]
for i in range(100,1000): a = i // 100 b = (i - 100*a) // 10 c = i % 10 if i == a**3 + b**3 + c**3: print(i) else: continue
[Reference Answer]
for i in range(100, 1000): sum = 0 temp = i while temp: sum = sum + (temp%10) ** 3 temp //= 10 #Note that floor is used here except ~ if sum == i: print(i)
2. The tricolor ball problem
There are red, yellow and blue balls, of which there are 3 red balls, 3 yellow balls and 6 green balls. First, mix the 12 balls into a box and pull out any 8 balls from it. Then program the color matches of the balls.
[Write it yourself!]
for a in range(0, 4): for b in range(0, 4): for c in range(2, 7): sum = a+b+c if sum == 8: print('The red ball is:', a, ' individual \t The yellow ball is:', b, ' individual \t The green ball is:', c, ' individual')
[Reference Answer]
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: # Note that the bottom is not a string concatenation, so no'+'oh~ print(red, '\t', yellow, '\t', green)
[Notes]
(1) [t] cannot be used alone, but use quotation marks [']:
Error:
print('The red ball is:', a, ' individual' \t 'The yellow ball is:', b, ' individual' \t 'The green ball is:', c, ' individual')
Correct:
print('The red ball is:', a, ' individual \t The yellow ball is:', b, ' individual \t The green ball is:', c, ' individual')