loumingyu-Python-day02
2021-08-29 本文已影响0人
__method__
比较运算符
逻辑运算符
## 比较运算符
a, b, c = 10, 3, 7
print(a >= b) #True
print(a > b) #True
print(a < b) #True
print(a <= b) #True
print(a == b) # False
print(a != b) # False
# 逻辑运算符 与 或 非
print((a > c) and (a > b)) # True &&
print((a > c) and (a < b)) # False
print((a > c) or (a > b)) # True ||
print((a < c) or (a > b)) # True
print((a < c) or (a < b)) # False
print(not (a < c)) # True !
程序执行结构
- 顺序结构
- 分支结构
- 循环结构
分支结构(条件语句)
if语句
age = int(input("请输入您的年龄"))
# TypeError: '>=' not supported between instances of 'str' and 'int'
if age >= 18 and age <=60:
print("恭喜你成年了, 可以去网吧了")
print("恭喜你成年了, 还不赶快行动")
elif age > 60 and age <=150:
print("退休啦")
elif age >=0 and age <=18:
print("你还是个宝宝")
else:
print("输入数据有误")
作业
BMI计算器
计算公式为:BMI=体重÷身高^2。(体重单位:千克;身高单位:米。)
猜拳游戏
# 猜拳游戏
from random import randint
computer = randint(1, 3) # [1, 3] 左闭右闭
# print(computer)
player = int(input("请出拳: ⽯头(1)剪⼑(2)布(3)"))
if (player==1 and computer==2)or(player==2 and computer==3)or(player==3 and computer==1):
print("玩家胜利啦")
elif player==computer:
print("平局")
else:
print("你真菜, 我赢了")
循环
# while
# while 条件:
# 循环体
i = 1
mySum = 0
while i <= 100:
print(i, end="\t")
mySum += i
i+=1
print()
print(mySum)
# break 跳出本层循环
# 累加和达到大于1000停止
i = 1
mySum = 0
while i <= 100:
if mySum > 1000:
break
mySum += i
i+=1
print()
print("mySum = ", mySum)
# continue 结束本次循环执行下一次循环
# 1~100之间所有奇数的累加和
i = 1
mySum = 0
while i <= 100:
if i%2 == 0:
i+=1
continue
mySum += i
i+=1
print()
print("mySum = ", mySum)
for 循环
# for循环
# for 临时变量 in 可迭对象:
# 循环体
# range(起始, 终止, 步长) 左闭右开区间 步长不能为小数
# for i in range(10):
# for i in range(1, 101):
# for i in range(1, 101, 10):
mySum = 0
for i in range(1, 101):
mySum +=i
print("mySum = ", mySum)
# # break 跳出本层循环
# # 累加和达到大于1000停止
mySum = 0
for i in range(1, 101):
if mySum > 1000:
break
mySum +=i
print("mySum = ", mySum)
# # continue 结束本次循环执行下一次循环
# # 1~100之间所有奇数的累加和
mySum = 0
for i in range(1, 101):
if i %2 == 0:
continue
mySum +=i
print("mySum = ", mySum)