python流程控制结构—if else 、while、for
'''
单分支
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("再接再厉,假期愉快!")
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")
xuefen=int(input("请输入您的学分:"))
grade=int(input("请输入您的成绩:"))
if xuefen>10:
if grade>80:
print('升班')
pass
else:
print("学分够了,成绩不达标")
pass
else:
print("学分预警")
pass
'''
***语法特点***
1.有初始值
2.条件表达式
3.变量【循环体内计数变量】的自增或者自减,否则会造成死循环
#使用条件:循环的次数不确定,依靠循环条件来结束
#目的:为了将相似或者相同的代码操作变的更加简洁,使的代码可以重复利用
# for
'''
index=1
while index<=100:
print(index)
index+=1
pass
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=" ")
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)
tags='我是一个中国人'
for item in tags:
print(item)
pass
sum=0
for data in range(1,101):
sum+=data
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
sum=0
for item in range(1,51):
if sum>100:
break
pass
sum+=item
pass
print("sum=%d"%sum)
sum=0
for item in range(1,101):
if item%2!=0:
continue
print("continue后的都不会执行")
pass
print(item)
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:
只要for中出现了break,else就不会执行,没出现else,else就会执行
account='wf'
pwd='123'
for i in range(1,3):
zh=input("请输入账号:")
mm=input("请输入密码:")
if account==zh and pwd==mm:
print("loading...")
break
pass
pass
else:
print("error")
pass
index=1
while index<=10:
print(index)
if index==6:
break
pass
index+=1
pass
else:
print("else执行了吗")
'''
猜年龄小游戏,有三点需求
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
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