# 分支语的几本书写
age = 15
if age < 18:
print("未成年人不能进出!") #tab输入四个空格
if 1 == 1 :
print("等于1")
else :
print("不等于1")
b = 1==1
print(b)
print(1 == 1.0)
print("abc".lower() == "ABC".lower())
print(" abc".strip() == "abc") #删除空格
print(str(1) == "1")
print(1 == int("1"))
# 特殊比较 数字和布尔表达式的比较
# 数字0代表False ,非0代表True
print(0 == False)
print(1 == True)
if 3-1 :
print("成立")
else :
print("不成立")
# 逻辑运算符 优先级 not > and > or
a = 3>1
b = 5<2
c = 8 == 8
d = 9 < 6
print(a and b)
print(a and c)
print(a or b)
print(not a )
print(not b)
# 优先级 not > and > or
r1= a and b or c and not b
print(r1)
r2=(a and (not b or c)) and d
print(r2)
# 多分支语句 elif
weight =input("输入体重:")
height = input("输入身高:")
bmi=int(weight)/pow(float(height),2)
print("bim="+str(bmi))
if(bmi <= 18.4):
print("偏瘦")
elif(bmi>18.4 and bmi<23.9):
print("正常")
elif(bmi>23.9 and bmi<=27.9):
print("过重")
else:
print("肥胖")
# 多重分支语句
high = input("请输入您测量的高压值:")
low = input("请输入您测量的低压值:")
high = int(high)
low = int(low)
if (low > 60 and low < 90) and (high > 90 and high < 140):
print("您的血压正常,请继续保持健康的生活习惯")
else:
if low <= 60:
print("您的低压过低,请注意补充营养。")
elif high <= 90:
print("您的高压过低,请加强锻炼,提高心肺功能")
else:
print("您的血压已经超标,请尽快就医")
# while循环的使用方法
'''
print("Python is the best language")
print("Python is the best language")
print("Python is the best language")
print("Python is the best language")
print("Python is the best language")
'''
#1. 定义循环的执行条件
#2. 编写要被执行的循环代码
#3. 编写修改执行条件的代码
i = 0
while i < 5:
print("Python is the best language")
i = i + 1
#阶乘计算器
num = input("请输入要计算的数值(1-100):")
num = int(num)
if num >= 1 and num <=100:
i = 1
result = 1 #结果
while i <= num:
#1: result = 1 , i = 1 , 结果=1
#2:result = 1 , i = 2 , 结果=2
#3: result = 2 , i = 3 , 结果=6
#4:result = 6 , i = 4 , 结果=24
#5:result = 24 , i = 5 , 结果=120
#20...
result = result * i
if i % 5 == 0:
print("{}:{}".format(i,result))#5的倍数打印
i = i + 1
#不使用缩进的代码,代表while循环结束后继续执行的语句
print("最终结果:{}".format(result))
else:
print("请输入1-100有效数字")
# continue 用于跳过当前循环的剩余语句
start = 101
end = 183
i = 100
while i <= 182:
i = i + 1
if i % 17 != 0:
continue
print(i) # 能被17整除的数输出
# break 关键字用来终止循环语句
i = 0
while i < 3:
mobile = input("请输入您要查询的手机号:")
i = i + 1
if mobile == "13312345678":
print("您的话费余额为158元")
break
print("感谢您的来电")
# 小技巧
#循环嵌套
'''
口口口口
口口口口
口口口口
口口口口
'''
j = 0
while j < 4:
i = 0
while i < 4:
print("口", end="") # 结尾不换行(原本一个print一行)
i = i + 1
j = j + 1
print("")
运行结果: