简体   繁体   中英

Assignment: Python 3.3: Using while loop to check for condition of attribute in class

I am trying to use a 'while' loop to check the condition of an attribute within a class. Specifically, I need to check the Odometer to see if any object reaches 500. The code is for a simulated race. So it needs to check the class as a whole, to see if the Odometer of ANY of the objects reaches 500 (or over), and when that happens, to stop adding to the Odometer, and declare a winner. Right now, though, the 'while' loop runs until each object Odometer reaches 500. Moving the 'while' loop to before the 'for' loop causes an error, saying that the car class does not have an Odometer attribute...

So my question is, how do I go about getting my 'while' loop to work for the class as a whole, rather than for each object separately? Or am I trying to tackle this completely wrong? I'm at a loss here, but I think I am approaching the issue correctly. Here is what I have, in it's entirety.

from random import randint
import time
class car:
    def __init__(self, Driver, Sponsor, Odometer=0, Speed=0):
        self.Driver = Driver
        self.Sponsor = Sponsor
        self.Odometer = Odometer
        self.Speed = randint(1,121)
drivers={'David': 'Burger King',
         'Travis': 'Dr Pepper',
         'Landon': 'Precon Marine',
         'Jamie': 'Cessna',
         'Juan': 'Target',
         'Ken': 'Federated Auto Parts',
         'Timmy': 'OXYwater',
         'Josh': 'MDS Transport',
         'Kurt': 'Furniture Row',
         'Casey': 'Geico',
         'Kasey': 'Farmers Insurance',
         'Jeff': 'Axalta',
         'Jimmie': 'Lowes',
         'Dale': 'National Guard',
         'Mike': 'Plinker Tactical',
         'Denny': 'FedEx',
         'Kyle': 'Mars Brands',
         'Matt': 'Husky',
         'Bobby': 'Kingsford',
         'Clint': '5-Hour Energy'
}
cars = [car(driver, sponsor) for driver, sponsor in drivers.items()]
for car in cars:
    while car.Odometer < 500:
        car.Odometer=car.Odometer+car.Speed*0.17
        car.Speed = randint(1,121)
        print(car.Driver, car.Odometer, car.Speed)

Maybe something like this?

from operator import attrgetter

cars = [car(driver, sponsor) for driver, sponsor in drivers.items()]

# we find the car with the max Odometer
car_max_odometer = max(cars, key=attrgetter("Odometer"))
while car_max_odometer.Odometer < 500
    for car in cars:
        car.Odometer=car.Odometer+car.Speed*0.17
        car.Speed = randint(1,121)
        print(car.Driver, car.Odometer, car.Speed)
    car_max_odometer = max(cars, key=attrgetter("Odometer"))
print("We have a winner! {}, {}, {}".format(car_max_odometer.Driver, car_max_odometer.Odometer, car_max_odometer.Speed))

You should also look into PEP8, your class name, variable names and white spaces don't follow convention.

In addition:

self.Speed = randint(1,121)

Should be:

self.Speed = Speed

Since you're passing speed as a keyword argument

If you want it to default to randint(1,121) when speed of 0 is passed in or if it's None then you can do

self.Speed = Speed or randint(1,121)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM