1练习

2知识点
2.1条件判断语句:if语句
1.单变量简单判断
python中非0和非空值为true,0和none为false
#if判断条件1:
if True:
#执行语句1
print("ture")
else:
#执行语句2
print("false")
2.单变量多重条件判断
case:判断分数,90-100为A,60-90为B,低于60为E
1)嵌套
score=77
if score>=90 and score<=100:
print("本次考试等级为A")
else:
if score>=60 and score<90:
print("本次考试等级为B")
else:
print("本次考试等级为E")
2)不嵌套,elif——>更好用
score=77
if score>=90 and score<=100:
print("本次考试等级为A")
elif score>=60 and score<90:
print("本次考试等级为B")
else:
print("本次考试等级为E")
3.多变量判断
判断性别、受否单身
#1代表男生,0代表女生
xingbie=1
#1代表单身,0代表有男女朋友
danshen=0
if xingbie==1:
print("男生")
if danshen==1:
print("单身")
else:
print("有男女朋友")
else:
print("女生")
if danshen==1:
print("单身")
else:
print("有男女朋友")
2.2随机数生成
#引入随即库
import random
#随机生成[3,4,5]中的一个数
x=random.randint(3,5)
print(x)
3练习代码&结果
a=int(input("请输入:剪刀(0)、石头(1)、布(2)"))
if a==0:
print("你的输入为:剪刀(0)")
elif a==1:
print("你的输入为:石头(1)")
else:
print("你的输入为:布(2)")
import random
x=random.randint(1,3)
print("随机生成数字为:",x)
if x>a:
print("哈哈,你输了!")
elif x==a:
print("咱们打平了!")
else:
print("恭喜,你赢了!")
结果:

这篇博客介绍了Python编程中的条件判断语句,包括单变量判断、多变量判断及多重条件的嵌套和非嵌套实现。此外,还讲解了如何使用random库生成随机数。通过实例展示了如何根据考试分数判断等级以及根据性别和单身状态进行判断。同时,给出了用户输入剪刀、石头、布游戏的示例,并实现了与计算机的随机选择进行比较。
2019

被折叠的 条评论
为什么被折叠?



