Python Auto Race Winner

I need to create a Python program that will determine the race winner using the odometer_miles = odometer_miles + speed * time equation and with a class called Car that has “common miles spaces”, “speed”, “driver name” and “sponsor” attributes.

The variable speed is a number from 0 to 120 (mph), which is randomly generated every minute. Then the equation is executed again, updating the odometer_miles variable.

Once the odometer_miles variable reaches 500 (or the closest value is greater than 500) miles, the race will end, and whichever rider (from group 20) reaches 500 is declared the winner. When a winner is determined, the program needs to type “driver name” and “sponsor”.

I think I created the class correctly, but the rest of the program far exceeds my capabilities. My textbook is less useless, and I have no way to contact my professor. I spent the last few hours trying to figure it out to no avail.

This is what I have so far:

 class Car: def __init__(self, odo_miles, speed, driver, sponsor): self.odo_miles = odo_miles self.speed = speed self.driver = driver self.sponsor = sponsor 

If someone can show me how to do this, with only two riders or enough so that I can complete for all 20 riders, I would always be grateful.

Thank you so much for your help!

+4
source share
1 answer

This class of car should have everything you need, note that the order in which you increase the speed of the riders will matter. In any case, here you can create a list / set of cars, and then skip it by applying updateMinute () for each, until you get a winner. You can exit the loop when updateMinute () returns True, and use the updated car to find the driver and sponsor.

 import random class Car: def __init__(self, odo_miles, speed, driver, sponsor): self.odo_miles = odo_miles self.speed = speed self.driver = driver self.sponsor = sponsor def updateMinute(): self.odo_miles += speed #I'm updating the distance before newSpeed #So that the original speed passed in is used if self.odo_miles > 500: return True self.speed = random.randrange(120) return False 

To start the list:

 while True: for c in cars: finished = c.updateMinute() if finished: print_relevant_stuff() return 
+1
source

Source: https://habr.com/ru/post/1485202/


All Articles