2.python------选择流程和循环流程

 一、选择流程

#猜拳小游戏
# 0:石头 1:剪刀 3:布
import  random  #直接导入产生随机数的模块
person=int(input('请出拳[0:石头 1:剪刀 3:布]:'))
computer=random.randint(0,2)
if person==0 and computer==1:
    print('恭喜你 你赢了 ')
    pass
elif person==1 and computer==3:
    print('恭喜你 你赢了')
    pass
elif person==2 and computer==0:
    print('恭喜你 你赢了')
    pass
elif person==computer:
    print('打成平手')
    pass
else:
    print('哈哈 你输了')
    pass
#if-else 的嵌套结构
# 一个阶段需要分阶段挥着分层次,做出不同的处理
#要执行内部的 if 语句 一定要外部的 if 语句满足条件才行

xuefen=int(input('请输入你的学分:'))
grade=int(input('请输入你的成绩:'))
if xuefen>=10:
    if grade>=80:
        print('你合格了')
        pass
    else:
        print('你的成绩不合格')
        pass
    pass
else:
    print('抱歉 你学分不够')

二、循环流程

while使用 主要适用于 对未知的循环次数  用于判断
for使用: 适用于的循环次数【可迭代对象的遍历】

while循环

while 条件表达式:
        代码指令
语法特点:
1.有初始值
2.条件表达式
3.变量【循环体内计数变量】的自增自减 否则会造成死循环
使用条件:循环次数的不确定性 是依靠循环条件来 结束
目的:为了将相似或者相同的到吗操作变得更加简洁,是的代码可以重复使用
#案例一 输出1-100之间的数据
index=1 #定义一个变量,必须赋值
while index<=100:
    print(index)
    index+=1
    pass
#案例二  做十次猜拳游戏
count=1
while count<=10:
    import random  # 直接导入产生随机数的模块

    person = int(input('请出拳[0:石头 1:剪刀 3:布]:'))
    computer = random.randint(0, 2)
    print('电脑出拳:%d'%computer)
    if person>2:
        print('出错了')
    elif person == 0 and computer == 1:
        print('恭喜你 你赢了 ')
        pass
    elif person == 1 and computer == 3:
        print('恭喜你 你赢了')
        pass
    elif person == 2 and computer == 0:
        print('恭喜你 你赢了')
        pass
    elif person == computer:
        print('打成平手')
        pass
    else:
        print('哈哈 你输了')
        pass
    count+=1
    pass
# 案例三  打印九九乘法表   循环嵌套
row=1
while row<=9:
    col=1
    while col<=row:
        print('%d*%d=%d'%(row,col,row*col),end=" ")  # end=‘ ’ 表示每输出一个不换行,空格隔开
        col+=1
        pass
    print('')   #换行效果
    row+=1
    pass
或者
row=9
while row>=1:
    col=1
    while col<=row:
        print('%d*%d=%d'%(row,col,row*col),end=" ")  # end=‘ ’ 表示每输出一个不换行,空格隔开
        col+=1
        pass
    print('')  #换行效果
    row-=1
    pass
# 案例四:打印直角三角形
row=1  #row逐行增加,等到8行就不用打印了
while row<=8:
    j=1
    while j<=row:
        print('*',end=' ')   #每个j打印完不换行,用空隔开
        j+=1
        pass
    print('')  #每个row打印完(每行),就换行
    row+=1
    pass
#第一行打印1个,第二行打印2个    共8行:12345678

#打印倒置直角三角形(方向相反的)
row=8  #row逐行减少  等到打印1就不用了
while row>=1:
    j=1
    while j<=row:
        print('*',end=' ')   #每个j打印完不换行,用空隔开
        j+=1
        pass
    print('')  #每个row打印完(每行),就换行
    row-=1
    pass
# 共八行,7654321
# 扩展练习案例五:打印等腰三角形
row=1
while row<=10:
    j=1
    while j<=10-row:
        print(' ',end='')
        j+=1
        pass
    k=1
    while k<=2*row-1:
        print('*',end='')
        k+=1
        pass
    print('')
    row+=1
    pass

for循环

是一种遍历操作,依次的取集合中的每个值
语法结构:
         for  临时变量  in  容器
                   执行代码块
tags='我是中国人'  #字符串本身就是一个字符类型集合
for item in tags:
    print(item)
    pass

#range 此函数可以生成一个数据集合列表
#range(起始:结束:步长)  步长不能为0,不包括结束值

#累加求和
sum=0
for data in range(1,101):  #输出1到100的值;左包含右不包含;遍历1到100的值
    sum+=data
    print(data,end='')
    pass
print('sum=%d'%sum)
#--------------for使用-----------------
#range() 中判断偶数,奇数
for data in range(50,201):   # 遍历50到200之间的数
    if data%2==0:  #逻辑运算符
        print('%d是偶数'%data)
        pass
    else:
        print('%d是奇数'%data)
        pass

#奇数偶数之和
sum = 0   #range函数end时不包括当前数值,所有注意101.
for i in range(0,101,2):  #偶数之和
    sum += i
print(sum)
#或者
sum = 0
i = 1
while i <= 100:
    sum += i
    i += 2    #奇数之和
print(sum)

三、continue和break

1. break:退出循环,代表中断结束  满足条件直接的结束所有循环
2. continue:退出本次循环,继续的进行下次循环(当continue的条件满足时,本次循环
剩下的语句将被跳过) 后面的循环继续
3. 这两个只能用在循环中
#---------break的使用-----------
sum=0
for item in range(1,51):
    if sum>100:               # 积累到某个值停止
        print('循环值执行到%d停止'%item)
        break  #大于100就退出循环,不再执行了
        pass
    sum+=item
    pass
print('sum=%d'%sum)
#---------continue的使用-----------
#1.找出奇数
for item in range(1,51):
    if item%2==0:
        continue
        pass
    print(item)
    pass

#2.走到e停止
for item in 'i love python':
    if item=='e':
        break       #在e出停止循环
        pass
    print(item)

#3.跳过e
for item in 'i love python':
    if item=='e':
        continue   #跳过e继续后面的循环
        pass
    print(item)


#4.用while循环输出1~100,在20停止循环
index=1
while index<=100:
    if index>20:
        break
        pass
    print(index)
    index+=1
    pass
#--------案例;用for来实现九九乘法表-------
for i in range(1,10):    #1到9
    for j in range(1,i+1):
        print('%d*%d=%d'%(i,j,i*j),end=' ')  #不用每个j都换行用空格分割
        j+=1
        pass
    print('')
    i+=1
    pass

#------for else用法---------
for item in range(1,11):
    print(item,end=' ')
    if item>=5:
        break
        pass
    pass
else:
    print('在上面循环中,只要出现break,那么else代码将不再执行')


#----登陆密码错误三次及以上,账号就会被锁定---
account='swy'     #必须是字符串
pwd='36456'
for i in range(3):
    zh=input('请输入你的账号:')
    mm=input('请输入你的密码:')
    if zh==account and mm==pwd:
        print('恭喜您登陆成功')
        break  #退出本次循环,不再执行
        pass
    pass
else:
    print('不好意思,您的账号已被锁定')

#------while else用法---------
index=1
while index<=10:
    print(index)
    if index==6:
        break
        pass
    index+=1
    pass
else:
    print('else执行了吗')    #没有,在break出现时停止循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值