流程控制主while,for,python画金字塔,画9*9乘法表

5.6 自我总结

一.流程控制while

1.while

while True:                 #while + 条件满足进行下面的循环
    age = input('age:')     #while 循环的内容

2.while...break

while True:                 #while + 条件满足进行下面的循环
    age = input('age:')     #while 循环的内容
    break                   #停止本次循环

补充

a = True        #循环语是子上而下的,当While中条件不满足时也会停止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--------- #相当于如果NUM的值等于1时候不进行下面的运算,但是不跳出While,返回while       print(num)
    

4.while...else

n = 1
while n < 3:
    print(n)
    n += 1            
else:
    print('else会在while没有被break时才会执行else中的代码')

5.while的嵌套

例如


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('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')

print('退出了while循环')

二.流程控制for(和while用法差不多)

1.for

2.for...break

3.for...continue

4.for...else

5.for的嵌套

三.for与while的区别

for和while条件者快一个是可控的一个是不可控的,while容易出现死循环

四.今日题目

1. 打印1-100之间的偶数:

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

2.猜年龄游戏升级版,有以下三点要求

  1. 允许用户最多尝试3次

  2. 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序

  3. 如何猜对了,就直接退出

     ```python
     #允许最多输入三次年龄
     #三次没有猜对让用户输入Yory表示继续,Norn表示退出
     #猜对直接退出
     time = 0
     ipt_time = 3
    
     while time < ipt_time+1:
         age =input('请输入您的年龄:')
         age =int(age)
         if age ==18:
             print('恭喜你猜对了')
             break
         elif age < 18:
             print('小了,请再试试')
         else:
             print('大了,请再试试')
         time += 1
         if time == 3:
             print('你想不想再玩')
             choose = input(f'(输入Y或y继续玩,输入N或n不想玩退出)')
             if choose =='Y'or choose == 'y':
                 time = 0
             elif choose == 'N' or choose == 'n':
                 time = 4
             else:
                 print('不听话乱输入\n请好好输入')
    
     ```

3.99乘法表

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 
#第一种
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='')
   #第二种改进
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='')
  #居然漏了下面结果(一直在想优化了)
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}={num_former*num_latter} ')
            continue
        print(f'{num_former}*{num_latter}={num_former*num_latter} ',end='')

4.打印如下所示金字塔

             # max_level=5
    *        # current_level=1,空格数=4,*号数=1 1 
   ***       # current_level=2,空格数=3,*号数=3 3 
  *****      # current_level=3,空格数=2,*号数=5 5 
 *******     # current_level=4,空格数=1,*号数=7
*********    # current_level=5,空格数=0,*号数=9

# 数学表达式
空格数 = max_level-current_level
*号数 = 2*current_level-1
'''
#方法1
print(f'    *')
print(f'   ***')
print(f'  *****')
print(f' *******')
print(f'*********')
#方法二
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'*********')
 #方法三
             # max_level=5            
    *        # current_level=1,空格数=4,*号数=1 1 = 0+1
   ***       # current_level=2,空格数=3,*号数=3 3 = 1+2
  *****      # current_level=3,空格数=2,*号数=5 5 = 2+3
 *******     # current_level=4,空格数=1,*号数=7 7 = 3+4
*********    # current_level=5,空格数=0,*号数=9 9 = 4+5
for row in range(6):
    A = (5-row)*' '
    B = (2*row+1)*'*'
    print(f"{A}{B}")
#再精简
for row in range(6):
    print((5-row)*' '+(2*row+1)*'*')
#漏了开头一行,
print('')
for row in range(6):
    print((5-row)*' '+(2*row+1)*'*')
#或者
for row in range(7):
    if row == 0:
        print('')
        continue
    print((5-(row-1))*' '+(2*(row-1)+1)*'*')

转载于:https://www.cnblogs.com/pythonywy/p/10821455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值