目录
1.单分支
if 条件
满足条件会发生什么
pass
2.双分支
if 条件表达式(比较运算等):
满足条件会发生什么
else:
否则会发生什么
小练习1
score=int(input('请输入您的成绩'))
if score>60:
print('及格')
elif score<90:
print('优秀')
else:
print('不及格')
print('程序运行结束')
#input获取的是str
'''注
1)if和else后面的冒号
2)else不缩进
3) Pass相当于流程的占位符
3.多分支
if 条件表达式(比较运算等):
满足条件会发生什么
elif 条件表达式(比较运算等):
满足条件会发生什么
else:
否则会发生什么
#逻辑图相当于二叉树
#else是选配,根据实际情况来选
小练习2
import random
# 对象一为计算机 二是人
person=int(input('请出拳 0 石头 1剪刀 2布'))
computer=random.randint(0,2)
if person==0 and computer==1:
print('you won')
pass
elif person==1 and computer==2:
print('you won')
pass
elif person==2 and computer==0:
print('you won')
pass
elif person==computer:
print('tie')
pass
else:
print('you lose')
print('程序运行结束')
4.嵌套
#相当于excel的IF,有多个条件筛选
if 条件表达式(比较运算等):
定义变量
if 条件表达式(比较运算等):
满足条件会发生什么
else:
会发生什么
else:
小练习3
#if-else
house=input('plz type your house')
if house=='Slytherin':
password=input('password?')
if password=='MU HAHA':
print('welcome back, MY LORD')
pass
else:
print('wrong,plz type again')
pass
pass
else:
print('get out you filthy muggle')
————————————————————————————————————————
小练习4
index=1
while index<=100:
print(index)
index+=1
pass
5.循环
5.1 while循环
#变量自增在循环体那个版块里
变量赋值
while 条件表达式:
print(变量)
变量数列(?)关系
pass
小练习5九九乘法表
row=9
while row>=1: #在小于1的时候停止
col=1
while col<=row:
print('%d*%d=%d'%(row,col,row*col),end=" ")
col+=1
pass
print()
row-=1
pass
#这里row和column不是真正print出来的行列不要搞混
这里是算数式里的指代
print的换行是由end=" "结束换行和print()决定的
5.2 for循环
遍历,依次取集合容器的值
for X in 容器
执行代码
6.其他细碎知识
- 快捷键ctrl+/ 切换文本化状态
- “end=”作为print()中的一个参数,会使该函数关闭“在输出中自动包含换行”的默认行为——end=’ '仅仅表示不换行
- end=’\t’——表示空四个字符,也称缩进,就是按一下Tab键
- print()相当于换行
- range()区间左闭右开
- shift+tab键可以取消多行缩进
【待解决问题】
1.pycharm怎么看程序一行一行运行的过程?
(想知道while循环的逻辑)
2.任务23是不是有问题 time==4,运行的时候输入N并不会停下, time=4才可以停下
#我写的
time=0
count=3
while time<=3:
age=int(input('请输入您的年龄'))
if age==23:
print('正确')
break #才对了 就不猜了 中断循环
elif age<23:
print('小了')
else:
print('大了')
time+=1
if time==3:
choose=input('是否继续 Y/N')
if choose =='Y' or choose =='y':
time=0
elif choose =='N' or choose =='n':
time=4
else:
print('请输入正确数值')
3.帮忙检查我用其他逻辑写的练习二(虽然我用值测试了没问题)
# 需要test5个区间值以测试是否正确 身高1.7 体重50过轻 60正常 80过重 90肥胖 100严重肥胖
count=1
while count<=5:
height=float(input('请输入您的身高'))
weight=float(input('请输入您的体重'))
BMI=weight/(height**2)
if BMI>=18.5:
if BMI<=25:
print('正常')
elif BMI<=28:
print('过重')
elif BMI<=32:
print('肥胖')
else:
print('严重肥胖')
else:
print('过轻')
count+=1
print('测试五次完毕')