说明
开头添加引用 datetime
模块
name
定义为书籍的名称
publisher
定义为出版社名称
price
定义为书籍价格
strDate
定义为出版日期
print
输出判断数据价格
逻辑运算符
例子:变量x为21,y为10,z为0,即x=21,y=10,z=0
符号 | 名称 | 表达式 | 方向 | 说明 | 实例 | 运算结果 |
---|---|---|---|---|---|---|
and | 逻辑与 | x and y | 从左到右 | 如果x为False或0,x and y返回False或0,否则返回y的计算值 | x and y,x and z,z and x | 10,0,0 |
or | 逻辑或 | x or y | 从左到右 | 如果x为True,则返回x的值,否则返回y的计算值 | x or y,x or z,z or x | 21,21,21 |
not | 逻辑非 | not x | 从右到左 | 如果x为True,则返回False。如果x为False,则返回Frue | not x,not y,not(x and y),not(x or y),not z | False,False,False,False,True |
任务3-2
import datetime
name="HTML5+CSS3网页设计与制作实战"
name1="HTML5+CSS3网页设计与制作实战"
name2="HTML5+CSS3移动Web开发实战"
publisher="人民邮电出版社"
publisher1="人民邮电出版社"
publisher2="高等教育出版社"
price="59.80"
priceTest="91.50"
price1="50.00"
price2="85.00"
strDate="2019-11-8"
date=datetime.datetime.strptime(strDate,"%Y-%m-%d")
strDateStart="2019-7-1"
dateStart=datetime.datetime.strptime(strDateStart,"%Y-%m-%d")
strDateEnd="2020-7-1"
dateEnd=datetime.datetime.strptime(strDateEnd,"%Y-%m-%d")
strDateTest1="2019-9-1"
dateTest1=datetime.datetime.strptime(strDateTest1,"%Y-%m-%d")
StrDateTest2="2020-10-5"
dateTest2=datetime.datetime.strptime(StrDateTest2,"%Y-%m-%d")
print("判断价格范围1:",price>=price and price<price2)
print("判断价格范围2:",priceTest>=price and priceTest<price2)
print("判断出版日期范围1:",date>dateStart and date<=dateEnd)
print("判断出版日期范围2:",dateTest1<dateStart or dateTest1>dateEnd)
print("判断出版日期范围3:",dateTest2>dateStart or dateTest2>dateEnd)
print("判断图书名称与出版社:",name==name1 and publisher==publisher1)
print("判断价格与出版日期:",price>=price1 and price<price2 and date>dateStart and date<=dateEnd)
{/tabs-pane}
{tabs-pane label="运行成功效果"}
{/tabs-pane}
评论 (0)