一、三大流程——顺序、选择、循环
二、顺序结构
python是脚本语言,从上而下、自左向右执行。
三、选择结构
1.单分支
if 条件:
#执行条件满足的语句
*1.遇到冒号必须缩进
*2.不要混用空格,Tab键(1个Tab=4个空格)
2.单分支
if 条件:
#执行条件满足的语句
else:
#执行不满足的语句
3.多分支
if 条件1:
#执行条件1的语句
elif 条件2:
#执行条件2的语句
eilf 条件3:
#执行条件3的语句
例题
1、BMI
a=float(input(“请输入你的身高:”))
b=float(input(“请输入你的体重:”))
BMI=b/(a*a)
print(BMI)
if BMI<18.5:
print(“你的体重太轻”)
elif BMI>=18.5 and BMI<24:
print(“正常”)
elif BMI>=24 and BMI<27:
print(“过重”)
elif BMI>=27 and BMI<30:
print(“轻度肥胖”)
elif BMI>=30 and BMI<35:
print(“中度肥胖”)
else:
print(“中度肥胖”)
2、从1~10猜数字
import random
computer=random.randint(0,10)
i=1
count=i
while i<=5:
userdata = int (input (“请输入一个数字”))
if computer>userdata:
print("你输的小了")
elif computer
print("你输的大了")
else:
print("你猜对了")
i+=1
else:
print(“你没机会了!”)
原文链接:https://blog.csdn.net/yulong0816/article/details/112634365