''' Welcome to LearnPython.NET File Name: PDataType.py Download from:https://www.learnpython.net/cn/python-code-samples.html Author: LearnPython.Net Editor: CoderChiu ''' from enum import Enum #定义车辆的类型; class VehicleType(Enum): Car = 1 Ambulance = 2 #定义路段 class ExpresswayLocation(Enum): ShenZhen = 1 HuiZhou = 2 #检查速度是否合法(小于等于80KM/H合法); def ExpresswaySpeedCheckinShenZhen(nSpeed): if nSpeed <= 80 : #返回 True表示合法; return True else: #返回 False表示非法; return False #当然速度79,检查是否合法; print(ExpresswaySpeedCheckinShenZhen(79)) #检查速度是否合法(60~120KM/H合法); def ExpresswaySpeedCheckinHuiZhou(nSpeed): if (nSpeed >= 60) and (nSpeed <= 120) : #返回 True表示合法; return True else: #返回 False表示非法; return False #当然速度120,检查是否合法; print(ExpresswaySpeedCheckinHuiZhou(120)) #救护车、小汽车; def ExpresswaySpeedCheck(nSpeed, enumCarType, enumLocation): if enumCarType == VehicleType.Ambulance: return True else: if enumLocation == ExpresswayLocation.ShenZhen: return ExpresswaySpeedCheckinShenZhen(nSpeed) elif enumLocation == ExpresswayLocation.HuiZhou: return ExpresswaySpeedCheckinHuiZhou(nSpeed) else: return False enumCarType = VehicleType.Car enumLocation = ExpresswayLocation.ShenZhen #小汽车当然速度79,运行在深圳,检查是否合法; print(ExpresswaySpeedCheck(79, enumCarType, enumLocation)) #小汽车当然速度120,运行在惠州,检查是否合法; print(ExpresswaySpeedCheck(120, VehicleType.Car, ExpresswayLocation.HuiZhou)) #救护车当然速度160,运行在惠州,检查是否合法; print(ExpresswaySpeedCheck(160, VehicleType.Ambulance, ExpresswayLocation.HuiZhou))