This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
我需要创建一个python程序,它将使用方程式odometer_miles = odometer_miles + speed * time和一个名为Car的具有“总里程表英里数”、“速度”、“驾驶员姓名”和“赞助商”属性的类来确定比赛的获胜者。
speed变量是每分钟随机生成的0到120(mph)之间的数字然后再次执行方程,更新odometer_miles变量。
一旦一个odometer_miles变量达到500英里(或最接近的值大于500英里),比赛就结束了,无论哪个参赛者(20人中的一个)达到500英里都被宣布为赢家。当确定胜出者时,程序需要打印“驱动程序名”和“赞助商”。
我认为我已经正确地创建了类,但是程序的其余部分远远超出了我的能力。我的课本没什么用,我也没办法联系我的教授我花了几个小时试图弄清楚这件事,但没有结果。
这就是我目前所拥有的:

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

如果有人能告诉我如何做到只有两个赛车或足够我可以填补整个20个赛车要求,我将永远感激。
非常感谢你的帮助!

最佳答案

这个汽车类应该有你需要的一切,注意你增加赛车手的顺序将很重要。不管怎样,从这里您可以创建一个汽车列表/集合,然后循环它,对每个汽车应用updateMinute(),直到您有一个赢家当updateMinute()返回True时,可以中断循环,并使用当前更新的汽车来查找驱动程序和发起人。

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

浏览列表:
while True:
    for c in cars:
        finished = c.updateMinute()
        if finished:
            print_relevant_stuff()
            return

07-24 09:44
查看更多