The eighth day of learning python (process control master while,for,python draws pyramid, draws 9 * 9 multiplication)

5.6 self summary

I. process control

1.while

while True:                 #When + conditions are met, carry out the following cycle
    age = input('age:')     #Contents of the while loop

2.while...break

while True:                 #When + conditions are met, carry out the following cycle
    age = input('age:')     #Contents of the while loop
    break                   #Stop this cycle

supplement

a = True        #The loop language is child up and down. When the condition in while is not satisfied, it will stop the loop of the content below while
while a == True:
    age = input('age:')
    if type(age) == type('1'):
        a = False

3.while...continue

num = 0
num_time = 3
while num <num_time: ←--- |
    num +=1               | 
    if num == 1:          | 
        continue--------- #It is equivalent to that if the value of NUM is equal to 1, the following operations will not be performed, but the While print (NUM) will not be returned
    

4.while...else

n = 1
while n < 3:
    print(n)
    n += 1            
else:
    print('else Will be in while Not being break Only when else Code in')

5. Nesting of while

for example

while True:
    user_db = 'nick'
    pwd_db = '123'

    inp_user = input('username: ')
    inp_pwd = input('password: ')

    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')

        while True:
            cmd = input('Please enter the command you need:')
            if cmd == 'q':
                break
            print(f'{cmd} Function execution')
    else:
        print('username or password error')

print('Quit while loop')

2. Process control for

1.for

2.for...break

3.for...continue

4.for...else

5. Nesting of for

The difference between for and while

for and while conditions are fast, one is controllable, the other is uncontrollable, while conditions are prone to loop

Today's topic

1. Print even numbers between 1-100:

```python
for num in range(100):
    num += 1
    if num % 2 == 1:
        continue
    print(num)
```

2. There are three requirements for the upgraded age guessing game:

  1. Allow users to try up to 3 times

  2. After every three attempts, if you haven't guessed it correctly, ask the user if you want to continue playing. If you answer y or Y, continue to guess it three times, so as to repeat. If you answer N or N, exit the program

  3. If you are right, just quit

     ```python
     #Age can be entered up to three times
     #Three times of no guessing, let the user enter "Yory" to continue, and "Norn" to quit
     #Guess right, just quit
     time = 0
     ipt_time = 3
    
     while time < ipt_time+1:
         age =input('Please enter your age:')
         age =int(age)
         if age ==18:
             print('Congratulations. You're right')
             break
         elif age < 18:
             print('Small, please try again')
         else:
             print('Big, please try again')
         time += 1
         if time == 3:
             print('Do you want to play again')
             choose = input(f'(input Y or y Continue playing, input N or n Don't want to play exit)')
             if choose =='Y'or choose == 'y':
                 time = 0
             elif choose == 'N' or choose == 'n':
                 time = 4
             else:
                 print('Disobedient input\n Please input well')
    
     ```

    3. Print 9 * 9 multiplication table as follows:

1*1=1 
2*1=2 2*2=4 
3*1=3 3*2=6 3*3=9 
4*1=4 4*2=8 4*3=12 4*4=16 
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
#First kind
for row_1 in range(1):
    print(f'1*{row_1+1}')
for row_2 in range(2):
    print(f'2*{row_2+1} ',end='')
for row_3 in range(3):
    if row_3 == 0:
        print(end='\n'f'3*{row_3+1} ')
        continue
    print(f'3*{row_3+1} ',end='')
for row_4 in range(4):
    if row_4 == 0:
        print(end='\n'f'4*{row_4+1} ')
        continue
    print(f'4*{row_4+1} ',end='')
for row_5 in range(5):
    if row_5 == 0:
        print(end='\n'f'5*{row_5+1} ')
        continue
    print(f'5*{row_5+1} ',end='')
for row_6 in range(6):
    if row_6 == 0:
        print(end='\n'f'6*{row_6+1} ')
        continue
    print(f'6*{row_6+1} ',end='')
for row_7 in range(7):
    if row_7 == 0:
        print(end='\n'f'7*{row_7+1} ')
        continue
    print(f'7*{row_7+1} ',end='')
for row_8 in range(8):
    if row_8 == 0:
        print(end='\n'f'8*{row_8+1} ')
        continue
    print(f'8*{row_8+1} ',end='')
for row_9 in range(9):
    if row_9 == 0:
        print(end='\n'f'9*{row_9+1} ')
        continue
    print(f'9*{row_9 + 1} ', end='')
   #The second improvement
for num_former in range(1,10):
    for num_latter in range (1,num_former+1):
        if num_former ==num_latter:
            print(f'{num_former}*{num_latter} ')
            continue
        print(f'{num_former}*{num_latter} ',end='')
   

4. Print pyramid as follows

             # max_level=5
    *Current level = 1, number of spaces = 4, * number = 1 1 
   ***Current level = 2, number of spaces = 3, * number = 3 3 
  *****Current level = 3, number of spaces = 2, * number = 5 
 *******Current level = 4, number of spaces = 1, * number = 7
 *********Current level = 5, number of spaces = 0, * number = 9

#Mathematical expression
 Number of spaces = max'level-current'level
 *No. = 2 * current Fu level-1
'''
#Method 1
print(f'    *')
print(f'   ***')
print(f'  *****')
print(f' *******')
print(f'*********')
#Method two
for row_1 in range(6):
    if row_1 == 0:
        print(' ')
    elif row_1 == 1:
        print(f'    *')
    elif row_1 == 2:
        print(f'   ***')
    elif row_1 == 3:
        print(f'  *****')
    elif row_1 == 4:
        print(f' *******')
    elif row_1 == 5:
        print(f'*********')
 #Method three
             # max_level=5            
    *        # Current Fu level = 1, number of spaces = 4, number of * signs = 1 1 = 0 + 1
   ***       # current_level=2, number of spaces = 3, * number = 3 3 3 = 1 + 2
  *****      # current_level=3, number of spaces = 2, * sign = 5 5 = 2 + 3
 *******     # Current Fu level = 4, number of spaces = 1, * number = 7 7 = 3 + 4
*********    # Current Fu level = 5, number of spaces = 0, * number = 9 9 = 4 + 5
for row in range(6):
    A = (5-row)*' '
    B = (2*row+1)*'*'
    print(f"{A}{B}")
#Further streamlining
for row in range(6):
    print((5-row)*' '+(2*row+1)*'*')
#Missing the first line,
print('')
for row in range(6):
    print((5-row)*' '+(2*row+1)*'*')
#perhaps
for row in range(7):
    if row == 0:
        print('')
        continue
    print((5-(row-1))*' '+(2*(row-1)+1)*'*')

Keywords: Python

Added by karnetics on Sat, 16 Nov 2019 22:21:12 +0200