微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基本的python类和大型if语句

如何解决基本的python类和大型if语句

尝试为作业做一些代码。希望我们以每小时最快的速度打印汽车品牌的名称。但是,为了找出最快的速度,老师希望我们做大量的if陈述来找出答案。

class carspeed:
    def __init__ (self,distance,time):
        self.distance=distance
        self.time=time
    
    def cspeed(self):
        return (self.distance)//(self.time)
        
Ford=carspeed(120,1.75)
Ferrari=carspeed(100,1.20)
BMW=carspeed(205,2.35)
Porsche=carspeed(155,1.85)
Audi=carspeed(190,2.10)
Jaguar=carspeed(255,2.45)

print("Ford speed in MPH:",Ford.cspeed())
print("Ferrari speed in MPH:",Ferrari.cspeed())
print("BMW speed in MPH:",BMW.cspeed())
print("Porsche speed in MPH:",Porsche.cspeed())
print("Audi speed in MPH:",Audi.cspeed())
print("Jaguar speed in MPH:",Jaguar.cspeed())

a=Ford.cspeed()
b=Ferrari.cspeed()
c=BMW.cspeed()
d=Porsche.cspeed()
e=Audi.cspeed()
f=Jaguar.cspeed()

def max_of_speed (a,b,c,d,e,f):
    fastest=a
    if fastest<b:
        fastest=b
    if fastest<c:
        fastest=c
    if fastest<d:
        fastest=d
    if fastest<e:
        fastest=e
    if fastest<f:
        fastest=f
    return fastest

print("The brand with the highest MPH is:",fastest)

#the(输出

Ford speed in MPH: 68.0
Ferrari speed in MPH: 83.0
BMW speed in MPH: 87.0
Porsche speed in MPH: 83.0
Audi speed in MPH: 90.0
Jaguar speed in MPH: 104.0
The brand with the highest MPH is: 68.0

#期望的输出

Ford speed in MPH: 68.0
Ferrari speed in MPH: 83.0
BMW speed in MPH: 87.0
Porsche speed in MPH: 83.0
Audi speed in MPH: 90.0
Jaguar speed in MPH: 104.0
The brand with the highest MPH is: Jaguar

解决方法

不给变量起名字,而是向name添加新属性carspeed

class carspeed:
    def __init__ (self,name,distance,time):
        self.name = name
        self.distance=distance
        self.time=time
    
    def cars(self):
        return (self.distance)//(self.time)


c1=carspeed('Ford',120,1.75)
c2=carspeed('Ferrari',100,1.20)
c3=carspeed('BMW',205,2.35)

cars = [c1,c2,c3]
fastestCar = max(cars,key=lambda item: item.cars())  # use Python's max function the get the fastest car!
print(fastestCar)  # returns the instance!
print("The brand with the highest MPH is:",fastestCar.name)

出局:

<__main__.carspeed object at 0x1032e6c70>
The brand with the highest MPH is: BMW
,

您需要以某种方式保持与速度值相关联的品牌名称,以便以后打印。您已经有一个包含速度值的对象。为什么不向包含品牌名称的对象添加变量。然后,不要传递逻辑中的最终速度数字,而是传递每个品牌汽车的对象。

您可以在这里做更多的事情。代码的最大问题是,您不必将汽车的数量硬编码到代码中……也就是说,如果添加一辆汽车,所有代码都必须更改。您真正想要的是将您的汽车对象存储在列表或字典中,然后在该数据结构上进行迭代以进行处理,无论结构中有多少汽车,而无需关心有多少汽车。

这是您的代码的一个版本,可以解决所有这些问题,并为您提供所需的结果:

class car:
    def __init__(self,time):
        self.name = name
        self.distance = distance
        self.time = time

    def cspeed(self):
        return (self.distance) // (self.time)


cars = [
    car("Ford",1.75),car("Ferrari",1.20),car("BMW",2.35),car("Porsche",155,1.85),car("Audi",190,2.10),car("Jaguar",255,2.45)
    ]

for car in cars:
    print("%(name)s speed in MPH: %(speed)s" % {'name': car.name,'speed': car.cspeed()})

fastest = max(cars,key=lambda item: item.cspeed())

print("The brand with the highest MPH of %(speed)d is %(name)s"
        % { 'speed': fastest.cspeed(),'name': fastest.name})

结果:

Ford speed in MPH: 68.0
Ferrari speed in MPH: 83.0
BMW speed in MPH: 87.0
Porsche speed in MPH: 83.0
Audi speed in MPH: 90.0
Jaguar speed in MPH: 104.0
The brand with the highest MPH of 104 is Jaguar
,

您本可以以更好的方式完成此操作,但无需更改很多代码即可执行此操作,但请注意,这不是最佳的执行方式

class carspeed:
    def __init__ (self,time):
        self.distance=distance
        self.time=time
    
    def cspeed(self):
        return (self.distance)//(self.time)
        
Ford=carspeed(120,1.75)
Ferrari=carspeed(100,1.20)
BMW=carspeed(205,2.35)
Porsche=carspeed(155,1.85)
Audi=carspeed(190,2.10)
Jaguar=carspeed(255,2.45)

print("Ford speed in MPH:",Ford.cspeed())
print("Ferrari speed in MPH:",Ferrari.cspeed())
print("BMW speed in MPH:",BMW.cspeed())
print("Porsche speed in MPH:",Porsche.cspeed())
print("Audi speed in MPH:",Audi.cspeed())
print("Jaguar speed in MPH:",Jaguar.cspeed())

a=[Ford.cspeed(),'Ford']
b=[Ferrari.cspeed(),'Ferrari']
c=[BMW.cspeed(),'BMW']
d=[Porsche.cspeed(),'Porsche']
e=[Audi.cspeed(),'Audo']
f=[Jaguar.cspeed(),'Jaguar']

def max_of_speed (a,b,c,d,e,f):
    fastest = a[0]
    brand = a
    if fastest<b[0]:
        fastest=b[0]
        brand = b
    if fastest<c[0]:
        fastest=c[0]
        brand = c
    if fastest<d[0]:
        fastest=d[0]
        brand = d
    if fastest<e[0]:
        fastest=e[0]
        brand = e
    if fastest<f[0]:
        fastest=f[0]
        brand = f
    return brand[1]

print("The brand with the highest MPH is:",max_of_speed(a,f))
,

由于将所有车速放入一堆单独的变量中而不是某种集合中,因此您遇到了问题。我建议一本字典。这是您的代码的样子:

class Carspeed:
    def __init__ (self,time):
        self.distance=distance
        self.time=time
    
    def cspeed(self):
        return (self.distance)//(self.time)

# for shorter version of this function - see below
def max_of_speed(car_speeds):
    max_speed = None
    brand = None
    for name,value in car_speeds.items():
        speed = value.cspeed()
        if max_speed is None or speed > max_speed:
            brand = name
            max_speed = speed
    return brand

car_speeds = {
    'Ford': Carspeed(120,'Ferrari': Carspeed(100,'BMW': Carspeed(205,'Porsche': Carspeed(155,'Audi': Carspeed(190,'Jaguar': Carspeed(255,2.45),}

for name,value in car_speeds.items():
    print(f"{name} speed in MPH:",value.cspeed())

print("The brand with the highest MPH is:",max_of_speed(car_speeds))

打印输出:

Ford speed in MPH: 68.0
Ferrari speed in MPH: 83.0
BMW speed in MPH: 87.0
Porsche speed in MPH: 83.0
Audi speed in MPH: 90.0
Jaguar speed in MPH: 104.0
The brand with the highest MPH is: Jaguar

我已尝试避免在上面的代码中使用任何过于复杂的内容,但这是max_of_speed函数的单行版本,您可以根据需要使用它。

def max_of_speed(car_speeds):
    return max(car_speeds,key=lambda name:car_speeds[name].cspeed())
,

只需按照您的代码和说明的方式进行,添加以下作业即可满足您的期望。

Output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。