python格式化运算符与循环 | 基础二

· Special

1. 1 循环

1.2练习

while True: #False #bool判断
        ....

while 1>2: #判断
        ....

flag =True  #变量
while flag:
        ...

flag = True
while flag: #while里改变量
        print('...')
        flag = False

#通过某个值的更新,控制循环的次数

num = 1

while num < 5:
        print('...')
        num = 5

num = 1

while num < 5:
        print('...')
        num = num + 1
if ...:
        pass
else:
        ....
#可以正选pass 反选!=

1.3 break中断

while True:
    name = input("Enter your name: ").strip()
    pwd = input("Enter your password: ").strip()
    if name == 'wupeiqi' and pwd == '123456':
        print(f'{name}登录成功')
        break
    else:
        print('登陆失败,请重试')

print('系统结束')

1.4 continue

#从0到100 绕过7

i = 1
while True:
    if i == 7:
        i = i+1
        continue
    if i == 101:
        break
    print(i)
    i = i+1

1.5 while else

#while else 循环完(当条件不再成立时)后执行else代码
i = 1
while i < 5:
    print(i)
    i += 1
print('666')
while True:
        print(123)
        break
else:
        print(6)

错题本

2.1 字符串格式化

2.1.1占位符

2.1.2 format

  1. 变量继承
  2. %继承
text = '%s,今年已经%d岁了'
data1 = text %('alex',30)
text = '你好{0},我是{1}'
data1 = text.format('alex',30)

2.1.3 f格式化

name = 'alex'
#message_05 = f'hi,{name},你的年龄是{20 + 1}' 代表结果不出=
message_05 = f'hi,{name},你的年龄是{20 + 1 = }'
print(message_05)

#结果
hi,alex,你的年龄是20 + 1 =21 

1 1 1 1 1

16 8 4 2 1

# 进制转换
message_06 = f'你的年龄是{22:#b}'
print(message_06)

# 进制转换
message_07 = f'你的年龄是{22:#o}'
print(message_07)

# 进制转换
message_08 = f'你的年龄是{22:#x}'
print(message_08)
#在括号内使用函数方法
name = 'alex'
message_09 = f'{name.upper()}喜欢大铁锤'
print(message_09)

3.1 运算符

3.1.1算数运算符

3.1.2比较运算符

3.1.3赋值运算

3.1.4成员运算

逻辑运算

flag = 1>2 #Flase
if not flag: #not False = True
    print(1)

运算优先级

if 3+2 > 1:
        pass
if 1 > 2 and 2 < 10:
        pass
if not 1 and 1 > 2 or 3 == 8:
    print(1)
else:
    print(2)
    
 
 
 # not 1 = False
 # 1 > 2 = False
 #False and False or 3 == 8
 #False or False
 #False
 2
 

面试题 算数大于比较大于逻辑

True and/or False
#1 比较运算在逻辑运算中输出True or False
v1 = name = 'alex' and pwd = '123' #v1= True and True

# 字符串在逻辑运算中,查看取决于谁(x),然后输出x的值
v2 = 'wupeiqi' and 'alex' # True and True 字符串只要非空为True
#v2取决于第二个True,所以v2输出alex

v3 = '' and 'alex' # False and True 取决于False v3 = ''

#and之中全True取最后一个值,有False取第一个False
#or之中
v4 = 1 or 8  # v4 = 1

v5 = 0 or 8 #v5 = 8

逻辑运算练习题and 看false or看true

v1  = 1 or 2 # v1 = 1
v2 = -1 or 3 # v2 = -1
v3 = 0 or -1 # v3 = -1
v4 = 0 or 100 # v4 = 100
v5 = '' or 100 # v5 = 100 
v6 = 'wupeiqi' or '' # v6 = 'wupeiqi'
v1 = 4 and 8 # v1 = 8
v2 = 0 and 6 # v2 = 0
v3 = -1 and 88 # v3 = 88
v4 = '' and 7 # v4 = ''
v5 = 'wupeiqi' and '' # v5 = ''
v6 = '' and 0 # v6 = ''
v7 = 0 and '中国' # v7 = 0 
#v1 = 0 or 4 and 3 or 7 or 9 and 6
v1 = 0 or 3 or 7 or 6
print(v1)

#v2 = 8 or 3 and 4 or 2 and 0 or 9 and 7
v2 =8 or 4 or 0 or 7
print(v2)

# 0 or 2 and 3 and 4 or 6 and 0 or 3
v3 = 0 or 4 or 0 or 3
print(v3)
v4 = not 8 or 3 and 4 or 2
# not 8(not 后面只能跟True?/False)
#v4 = False or 4 or 2
v4 = 4 

#猜数字
i = 0
while i < 3:
    i = i + 1
    guess_num =input('please enter your guess number: ')
    if guess_num==73:
        print('你猜对了')
        break
    else:
        print('你猜错了,剩下{}次机会'.format(3-i))

    if i==3: #限定失败,去掉这行可以无条件询问
        re_choice = input('是否继续Y/N')
        if re_choice=='Y':
            i=0
        elif re_choice=='N':
            break
            
  i=0
  while i<3:
          猜数字的代码
            
            通过y/n判断是否继续猜,通过格式化i来操作循环     

while循环代码

#输入三次猜 正反循环
#第一种方法
i = 0:
while i <3:
        i+=1
        
#第二种方法
i = 3
while i > 0:
        i-=1

where嵌套where,其中用exit结束整个循环

while True:
    i = 3
    while i > 0:
        i-=1
        guess_num = int(input(f'your like number: ').strip())
        if guess_num == 73:
            print('猜对了')
            break
        else:
            print('猜错了,还剩{}次机会'.format(i))

    #必须输入正确标志判断输入
    while True:
        re_choice = input('请输入是否继续Y/N').strip()
        if re_choice == 'Y' or re_choice == 'y':
            print('开始下一次循环')
            break
        elif re_choice == 'N' or re_choice == 'n':
            print('结束循环')
            exit()

        else:
            print('输入错误,正在重试')
            
            
  where True:
          #代码A
          where 条件:
                    pass
            #代码B判断A是否继续循环
            where True:
                    #如果继续
                    if 条件:
                            break #结束当前循环
                    elif 条件:
                            exit() #直接退出全部代码
                    else:
                            print("输入错误") #重新执行代码
                    

python


评论