python3流程控制相关介绍

1.if
if语法

if	条件:
	代码
	。。。
elif 条件:
	代码
	。。。
else:
	代码
	。。。

注意:
1.条件可以是任意表达式,但执行结果必须为布尔值
在if判断中所有的数据类型也都会自动转换成布尔类型:
None,0,空(空列表,空字符串,空字典等)都会转换成False,其余都为True。

2.while循环
python中有while与for两种循环机制,其中while循环称之为条件循环,语法如下:

while 条件:
	代码
	代码
	。。。

注意:while+else
当while循环正常执行完并且中间没有被break的话,就会执行else后面的语句,所以我们可以用来验证循环是否正常结束。

3.for循环
语法

for 变量名  in  可迭代对象:
	代码
	代码
	。。。

注意:字符串,列表,字典都是可迭代对象

练习

age_of_girl=30
if age_of_girl >= 30:
    print('阿姨好')

if age_of_girl >30:
    print('阿姨好')
else:
    print('小姐好')

age_of_girl = 18
height = 171
weight = 99
is_pretty = True
success=False
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
    print('表白')
    if success==True:
        print('在一起')
    else:
        print('...')
else:
    print('阿姨好')

score = input('请输入你的分数: ')
score = int(score)
if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')

name=input('请输入用户名: ')
password=input('请输入密码: ')
password=int(password)
if name=='zhaowei' and password==123:
    print('登录成功')
else:
    print('账户名或密码错误')
#!/bin/env python3
根据用户输入的内容打印权限
'''
zhaowei--->超级管理员
tom---->普通管理员
Jack,rain---->业务主管
其他----->普通用户
'''
name=input('请输入用户名: ')
if name == 'zhaowei':
    print('超级管理员')
elif name == 'tom':
    print('普通管理员')
elif name == 'Jack' or name == 'rain':
    print('业务主管')
else:
    print('普通用户')
username="jason"
password="123"
nums=0
while nums<3:
    in_name=input('请输入用户名: ')
    in_psd=input('请输入密码: ')
    if in_name==username and in_psd==password:
        print('登录成功')
        # nums=3
        while True:
            cmd=input('请输入要执行的操作: ')
            if cmd=='q':
                break
            else:
                print(cmd+'正在执行......')
        break
    else:
        print('用户名或密码错误')
        nums+=1

count=0
while count<10:
    count+=1
    if count ==7:
        continue
    else:
        print(count)
else:
    print('循环正常结束')

count=0
while count<10:
    count+=1
    if count ==7:
        break
    else:
        print(count)
else:
    print('循环正常结束')


number=100
while number<=100:
    if number%7==0:
        print(number)
        break
    number-=1

age=18
count=0
while count<3:
    guess=input('>>:')
    guess=int(guess)
    if guess > age:
        print('猜大了')
    elif guess <age:
        print('猜小了')
    else:
        print('猜对了')
    count+=1

for i in range(6):
    print(i)

遍历字典
for k in {'name':'zhaowei','age':18,'height':175}:
    print(k)

for i in range(3):
    for k in range(5):
        print('*',end='')
    print()#每打印5个换一次行

九九乘法表
for i in range(1,10):
    for j in range(1,i+1):
        print('{0}*{1}={2} '.format(i,j,i*j),end='')
    print()

max_level=5
for level in range(1,max_level+1):
    for i in range(max_level-level):
        print(' ',end='')
    for j in range(2*level-1):
        print('*',end='')
    print()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值