基本python类和大型If语句

2024-09-28 20:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试为作业编写一些代码。希望我们打印出每小时最快英里数的汽车品牌名称。但是为了找出最快的速度,老师希望我们做一个大型的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)

#(输出)

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

Tags: inselfifspeedprintfordaudimph
3条回答

你本可以用更好的方式来做,但是不需要改变很多代码,你也可以这样做,但是请注意,这不是最好的方式

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(),'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,b,c,d,e,f))

将新属性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 Carspeed:
    def __init__ (self,distance,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,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),
    }

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())

相关问题 更多 >