关卡一:
练习题1:
1.python中比较运算符都有哪些?
答:==、!=、<=、>=、<、>
2.a = 1, b = 2, 则(a == b) 、(a != b) 、(a > b) 、(a < b) 、(a >= b) 、(a <= b) 分别返回什么?
答:False、True、False、True、False、True
练习题2:
1.python中逻辑运算符有哪些? 它们之间有什么区别?
答:and:且、与,只要有一个False,最终结果就是False,只有两边都是Tru e,才会显示True
or:或,只要有一个True,最终结果就是True,只有两边都是False ,才会显示True
not:非,对值取反,True取反后是False,False取反后是True
2.使用if完成剪刀石头布程序
答:
import random
# 死循环
while True:
# 获取电脑出拳的随机数
computer = random.randint(1, 3)
# 获取玩家出拳的数
player = int(input("【1】:剪刀,【2】:石头,【3】:布,请输入你要出的拳头:"))
# 玩家赢了
if (computer == 1 and player == 2) or (computer == 2 or player == 3) or (computer == 3 and player == 1):
print("恭喜你赢了电脑")
# 平局
elif computer == player:
print("和电脑打成平手了,再接再厉")
# 玩家输了
else:
print("电脑赢了,继续努力啊")
练习题3:
1.编写代码,1-7七个数字,分别代表周一到周日,如果输入的数字是6或7,输出“周末”。
答:
while True:
# 获取输入的值
today = int(input("请输入1-7的数字,我们会返回判断后的值"))
# 判断是否是6/7,是的话输出周末
if today == 6 or today == 7:
print("周末")
2.编写代码,1-7七个数字,分别代表周一到周日,如果输入的数字是6或7,输出“周末”,否则输出“工作日”。
答:
while True:
# 获取输入的值
today = int(input("请输入1-7的数字,我们会返回判断后的值"))
# 判断是否是6/7,是的话输出周末
if today == 6 or today == 7:
print("周末")
# 否则输出工作日
else:
print("工作日")
3.编写代码,1-7七个数字,分别代表周一到周日,如果输入的数字是6或7,输出“周末”,如果输入的数字是1-5,输出“工作日”,如输入其他数字,输出“错误”。
答:
while True:
# 获取输入的值
today = int(input("请输入1-7的数字,我们会返回判断后的值"))
# 判断是否是6/7,是的话输出周末
if today == 6 or today == 7:
print("周末")
# 如果是1/2/3/4/5输出工作日
elif today == 1 or today == 2 or today == 3 or today == 4 or today == 5: