Reputation: 11
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
Upvotes: 1
Views: 223
Reputation: 71
Just going as per your way of code & expecatation, Below addition of assignment would fulfill your expectations.
Upvotes: 0
Reputation: 23089
You need to somehow keep the brand name associated with the speed values so you can print it later on. You already have an object containing the speed values. Why not add a variable to that object that holds the brand name. Then, instead of passing around the final speed number in your logic, pass around the objects for each brand of car.
There's a lot more you could do here. The biggest issue with your code is that you shouldn't have to hard code the number of cars into the code...that is, have all of your code have to change if you add one car. What you really want is to store your car objects in a list or a dictionary, and then iterate over that data structure to do your processing, handling however many cars are in the structure without caring how many there are.
Here's a version of your code that addresses all of that and gives you the result you desire:
class car:
def __init__(self, name, distance, time):
self.name = name
self.distance = distance
self.time = time
def cspeed(self):
return (self.distance) // (self.time)
cars = [
car("Ford", 120, 1.75),
car("Ferrari", 100, 1.20),
car("BMW", 205, 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})
Result:
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
Upvotes: 0
Reputation: 13079
You are coming up against problems due to putting all the car speeds into a bunch of separate variables instead of some kind of collection. I suggest a dictionary. Here is what your code could look like instead:
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))
This prints out:
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
I have tried to avoid using anything too complicated in the code above, but here is a one-line version of the max_of_speed
function that you could use instead if you want.
def max_of_speed(car_speeds):
return max(car_speeds, key=lambda name:car_speeds[name].cspeed())
Upvotes: 0
Reputation: 231
You could have done it in a better way but without changing a lot of your code you could do this but take note that this isn't the best way to do it
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))
Upvotes: 0
Reputation: 18106
Instead of giving variables the name, add a new attribute name
to 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)
Out:
<__main__.carspeed object at 0x1032e6c70>
The brand with the highest MPH is: BMW
Upvotes: 1