samedi 12 septembre 2020

Basic python class and large if-statement

Trying to do some code for an assignment. wants us to print the name of the brand of the car with the fastest miles per hour. But to figure out the fastest the teacher wants us to do a large if statement to find it out.

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

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

#desired output

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

Aucun commentaire:

Enregistrer un commentaire