|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
钓鱼记录
fish_record = "鲫鱼5条、鲤鱼8条、鲢鱼7条、草鱼2条、黑鱼6条、乌龟6只"
if fish_record[0 : 2] == "乌龟":
print("是乌龟吗?,是" + fish_record[0:2])
elif fish_record[5 : 7] == "乌龟":
print("是乌龟吗?,是" + fish_record[5:7])
elif fish_record[10 : 12] == "乌龟":
print("是乌龟吗?,是" + fish_record[10:12])
elif fish_record[15 : 17] == "乌龟":
print("是乌龟吗?,是" + fish_record[15:17])
elif fish_record[20 : 22] == "乌龟":
print("是乌龟吗?,是" + fish_recoed[20:22])
elif not fish_record[25 : 27] != "乌龟":
if int(fish_record[27]) %2 == 0:
print("找到乌龟了, 是%d只,偶数" % (int(fish_record[27])))
else:
print("找到乌龟了, 是%d只,奇数" % (int(fish_record[27])))
“颜值更高”什么意思,我没听过课不知道什么意思
要求是什么也不清楚
只能解释红色代码的意思了
if 的条件是 表达式 not fish_record[25 : 27] != "乌龟"
这里首先要知道在python中比较类的运算符(< > == !=)是优先于逻辑运算符(and or not)的
所以说这个表达式等同于 not (fish_record[25 : 27] != "乌龟")
fish_record[25 : 27] != "乌龟" 这个表达式意思是列表中25-27内容不是“乌龟”,实际情况内容正是“乌龟”,得到的结果是 False
前面加上not ,整个表达式 not fish_record[25 : 27] != "乌龟" 输出结果就是 True
所以if判断条件成立,继续判断 int(fish_record[27]) %2 == 0 即 列表中27位是奇数还是偶数,对应输出结果
not fish_record[25 : 27] != "乌龟"这个表达式可以简化成 fish_record[25 : 27] == "乌龟"
|
|