版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hu_junhua/article/details/86626277
1.if语句
if 要判断的条件 :
条件成立时,要做的事情(连续四个空格)
if语句以及下方带缩进的代码看成一个整体
1.if语句
age = int(input("请输入年龄:"))
if age >= 18: #if语句 下面缩进的是if语句完整的代码块,小箭头中间是完整的代码块
print("ojbk")
else:
print("hujunhua")
print("nihao")
2.if-elif-else语句
holiday_name = input("请输入节日名称:")
if holiday_name == "情人节":
print("情人节")
elif holiday_name == "圣诞节":
print("圣诞节")
elif holiday_name == "春节":
print("春节")
else:
print("每天都是节日啊")
3.if嵌套
has_ticket = True
knife_length = 30
if has_ticket:
print("有票,请进行安检")
if knife_length > 20:
print("通过安检")
else:
print("没通过安检")
else:
print("请先买票")
2.逻辑运算
逻辑运算符
与:and
或:or
非:not
1.与运算符
age = int(input("请输入年龄:"))
if age >= 0 and age <= 120: #与运算符and
print("年龄正确")
else:
print("年龄错误")
2.或运算符
python_score = 80
c_score = 59
if python_score > 60 or c_score > 60: #或运算符 or
print("考试通过")
else:
print("考试不合格")
3.非运算符
is_employee = False #非运算符 not
if not is_employee:
print("不是本公司员工")
3.产生一个随机数
import random//导入随机数的工具包
random.randint(a, b) #生成的随机数 a<=n<=b
在导入工具包的时候,应该将导入的语句放在文件的顶部,因为
这样可以方便下面的代码在任何需要的时候,使用工具包中的工具
import random # 导入随机数的工具包
player = int(input("请输入你的拳头:"))
computer = random.randint(1, 3) #产生1到3的随机数
print("电脑出的拳头:%d" % computer)
print(type(computer))
# 石头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("失败")