问题描述
课程作业是:
定义一个名为Car的类,其中包含以下属性:
总里程里数
以英里/小时计的速度
司机名称
赞助商
总的里程表里程和速度应该初始化为零。
创建一个包含随机(或真实的20个独特车辆的列表(链接到外部站点)。链接到一个外部网站。)驱动程序和赞助商名称。
您的主程序应该模拟比赛中车辆的进度。每个模拟分钟,车辆选择1到120之间的新随机速度,并使用以下等式每分钟更新它们的里程表里程:
odometer_miles = odometer_miles + speed *时间
由于速度以英里/小时为单位,因此时间也应以小时为单位(1分钟为1/60小时)。
第一辆达到500英里的汽车应该通过打印驾驶员名称和赞助商名称来宣布获胜者。
包括您看到的班级定义中的任何有用方法适合。
代码在Python 3.x中
The class assignment is:
Define a class called Car with the following attributes:
Total Odometer Miles
Speed in miles per hour
Driver Name
Sponsor
The total odometer miles and speed should be initialized to zero.
Create a list of 20 unique vehicles with random (or real (Links to an external site.)Links to an external site.) driver and sponsor names.
Your main program should simulate the progress of the vehicles in the race. Every simulated minute, the vehicles pick a new random speed between 1 and 120, and their odometer miles are updated every minute using this equation:
odometer_miles = odometer_miles + speed * time
Since speed is in miles per hour, time should be in hours as well (1 minute is 1/60th of an hour).
The first car to reach 500 miles should be declared the winner by printing the driver name and sponsor name.
Include any useful methods in your class definition that you see fit.
Code is in Python 3.x
# Nascar
from random import randint
import time
driverList = ["John", "Smith", "Alex", "Travis", "Bob", "James", "Dan", "David", "Mike", "Cody", "Kyle", "Trent", "Tristen", "Seth", "Brant", "Jacob", "Chase", "Ian", "Colter", "Austin"]
sponsorList = ["Target", "Walmart", "Cisco", "Costco", "Albertson", "Lumber Co", "Exxon", "Wonder Bread", "Taco Bell", "Taco John", "McDonalds", "Burger King", "Wendy's", "Safeway", "Town Pump", "Dodge", "Ford", "Chevrolet", "Porshe", "Nissan"]
class Car:
def __init__(self, odometer, speed, driver, sponsor):
self.odometer = odometer
self.speed = speed
self.driver = driverList
self.sponsor = sponsorList
def main():
odometer = 0
print("Let the race begin!")
while odometer < 500:
Nascar = {driverList , sponsorList}
time.sleep(3)
speed = randint(1, 121)
odometer = odometer + speed * 0.17
if odometer < 500:
print("So far " + Nascar[each_sponsor] + " has traveled '{}' miles.".format(odometer))
else:
print("The winner is " + Nascar[each_sponsor] + ", sponsored by " + each_sponsor)
return
main()
我尝试了什么:
我试图找到资源并尝试纠正错误而没有运气。
What I have tried:
I have tried finding resources and trying to correct the error with no luck.
推荐答案
Traceback (most recent call last):
File "a.py", line 27, in <module>
main()
File "a.py", line 18, in main
Nascar = {driverList , sponsorList}
TypeError: unhashable type: 'list'
问题出在 Nascar = {driverList,sponsorList}
。另外我不明白 Nascar [each_sponsor]
应该做什么。
The issue is at Nascar = {driverList , sponsorList}
. Also I don't understand what Nascar[each_sponsor]
is supposed to do.
这篇关于纳斯卡计划 - 我怎样才能让这个工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!