python流程控制结构---if else 、while、for

python流程控制结构—if else 、while、for


#一、选择流程————if 选择分支语句

'''
单分支
if 条件表达式: 比较运算符 逻辑运算符/符合的条件表达式
   代码指令
   ....
  '''
score=60
if score <=60:
    print("你的成绩为:\n%d"%(score))
    print('failed')
    pass #空语句
print('语句运行结束')
'''
---------------------------------
双分支
if 条件表达式: 比较运算符 逻辑运算符/符合的条件表达式
   代码指令
   ....
else:
    代码指令
'''
score=60
if score>60:
    print("successed")
    pass
else:
    print("failed")
    pass
--------------------------

'''
#多分支
#1.每个条件互斥
#2.至少有2种情况
#elif一定要写条件和语句
#else 可选,尽量用,避免遗漏情况
'''
score=int(input("请输入您的成绩:"))
if score<60:
    print("failed")
    pass
elif score<80:
    print("ok")
    pass
elif score<90:
    print("good")
    pass
else:
    print("very good")
    pass

print("再接再厉,假期愉快!")

#猜拳游戏 0-石头、1-剪刀、2-布

import random
#计算机 人

person=int(input("请出拳:[0-石头、1-剪刀、2-布]"))
computer=random.randint(0,2)

if person ==0 and computer==1:  #多条件
    print("win")
    pass
elif person==1 and computer ==2:
    print("win")
    pass
elif person==2 and computer ==0:
    print("win")
    pass
elif person==computer:
    print("no win no lost")
else:
    print("lost")

#if else 的嵌套使用
xuefen=int(input("请输入您的学分:"))
grade=int(input("请输入您的成绩:"))
if xuefen>10:
    if grade>80:
        print('升班')
        pass
    else:
        print("学分够了,成绩不达标")
    pass
else:
    print("学分预警")
    pass

#二、循环语句
#循环的分类:while、for
#1)while 条件表达式:
    #代码指令
'''
***语法特点***
1.有初始值
2.条件表达式
3.变量【循环体内计数变量】的自增或者自减,否则会造成死循环
#使用条件:循环的次数不确定,依靠循环条件来结束
#目的:为了将相似或者相同的代码操作变的更加简洁,使的代码可以重复利用
# for
'''
#输出1-100之间的数据

index=1 #定义一个变量,必须要赋值
while index<=100:
    print(index)
    index+=1
    pass

# 多次 猜拳游戏 0-石头、1-剪刀、2-布

import random
#计算机 人

count=1
while count<=10:

    person=int(input("请出拳:[0-石头、1-剪刀、2-布]"))
    computer=random.randint(0,2)

    if person ==0 and computer==1:  #多条件
        print("win")
        pass
    elif person==1 and computer ==2:
        print("win")
        pass
    elif person==2 and computer ==0:
        print("win")
        pass
    elif person==computer:
        print("no win no lost")
    else:
        print("lost")
    count+=1
    pass

#打印九九乘法表
row=1
while row<=9:
    col=1
    while col<=row:
        print("%d*%d=%d"%(row,col,row*col),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=7
while row>=1:
    j=1
    while j<=row:
        print("*",end=' ')
        j+=1
        pass
    print()
    row-=1
    pass


row=1
while row<=5:
    j=1
    while j<=5-row: #控制打印空格的个数
        print(' ',end=' ')
        j+=1
        pass
    k=1
    while k<=2*row-1: #控制打印*号
        print('*',end=' ')
        k+=1
        pass
    print()
    row+=1
    pass

2)#for循环
#语法特点:遍历操作,依次取集合容器中的每个值
#for 临时变量 in 容器:
#    执行代码块
tags='我是一个中国人' #字符串类型本身就是一个字符类型的集合
for item in tags:
    print(item)
    pass
#range函数:生成数据集合列表
#range(起始值,结束值,步长)  #步长不为0,默认为1

sum=0
for data in range(1,101):  #左闭右开
    sum+=data #求累加和
    #print(data,end=' ')
    pass
print("sum=%d"%sum)
print(-------------------------for的使用-----------)


for data in range(50,201):
    if data %2==0:
        print('%d是偶数'%data,end=' ')
        pass
    else:
        print("%d是奇数"%data)
        pass
    pass

#3)break continue
#break 代表中断结束 满足条件直接结束本层循环
#continue:结束本次循环,继续进行下次循环
#这两个关键字只能用在循环中
#break 的使用
sum=0
for item in range(1,51):
    if sum>100:

        break
        pass
    sum+=item
    pass
print("sum=%d"%sum)
#continue的使用
sum=0
for item in range(1,101):
    if item%2!=0:  #满足条件直接跳出本层循环
        continue
        print("continue后的都不会执行")
        pass
    print(item) #不满足则print
    pass


for item in 'i love python':
    if item=='e':
        break
        pass
    print(item)
    pass

for item in 'i love python':
    if item=='e':
        continue
        pass
    print(item)

index=1
while index <=100:
    if index >20:
        break
        pass
    print(index,end=' ')
    index+=1


for i in range(1,10):
    for j in range(1,10):
        if i>=j:
            print("%d*%d=%d"%(i,j,i*j),end=' ')
            pass
        if i<j:
            break
            pass
    print() #控制换行
    pass

for: else:  #else 也是for中的一部分
只要for中出现了breakelse就不会执行,没出现elseelse就会执行


account='wf'
pwd='123'

for i in range(1,3):
    zh=input("请输入账号:")
    mm=input("请输入密码:")
    if account==zh and pwd==mm:
        print("loading...")
        break #退出本层循环,下面的else也是for中的一部分,所以else也不执行
        pass
    pass
else:
    print("error")
    pass

#while else 只要while中没有出现break,else就会执行
#只要出现了break,else就不会执行
index=1
while index<=10:
    print(index)
    if index==6:
        break
        pass
    index+=1
    pass
else:
    print("else执行了吗")




#day2练习题

'''
猜年龄小游戏,有三点需求
1.允许用户最多尝试3次
2.每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y,就继续让其猜3次,以此往复,如果回答N或n,就退出程序
3.如何猜对了,就直接退出
'''


import random
age=random.randint(0,3)
print(age)
j=3
for i in range(1,4):
    answer=int(input("请输入您的答案:"))
    if age==answer:
        print("bingo")
        break
        pass
    j-=1
    print("您还有%d机会"%j)
    pass
else:
    print("您失败了")


times=0
count=0
while times<=3:
    age=int(input("请输入您的答案:"))
    if age==25:
        print("bingo")
        break
        pass
    elif age>25:
        print("太大了")
        pass
    else:
        print("太小了")
        pass
    times+=1
    if times==3:
        choose=input("您继续猜吗 Y/N?")
        if choose=='Y' or choose=='y':
            time=0 #重置times为初始值
            pass
        if choose=='N' or choose=='n':
            times==4
            pass
        else:
            print("请输入正确标记")
            pass
        pass
    pass

'''

#练习题2
小王身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小王计算他的BMI指数,
并根据BMI指数:
低于18.5过轻
18.5-25:正常
25-28:过重28-32:肥胖
高于32:严重肥胖
用if-elif判断并打印结果
'''

height=1.75
weight=80.5
BMI=weight/(height**2)
if BMI<=18.5:
    print("BMI:%d,过轻"%BMI)
    pass
elif BMI<=25:
    print("BMI:%d,正常" %BMI)
    pass
elif BMI<=32:
    print("BMI:%d,过重" %BMI)
    pass
else:
    print("BMI:%d,严重肥胖" %BMI)
    pass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值