white_cade_hw8

py

School

University of Texas *

*We aren’t endorsed by this school

Course

311C

Subject

Electrical Engineering

Date

Feb 20, 2024

Type

py

Pages

1

Uploaded by DeaconWaterBuffalo1027

Report
import random class Car: def __init__(self,n,s,d): self.name = n self.speed = random.randrange(60,100) self.max_distance = random.randrange(450,550) #set self.name to name #set self.speed to a random number between 60 and 100 #set self.max_distance to a random number between 450 and 550 def __str__(self): return self.name+ self.speed+ self.max_distance # return a string formatted with info for the name, speed, and max distance class Race: def __init__(self): self.distance= 500 #set the distance to 500 def startRace(self,racers): racer_list=[] for i in range(0,18): car= ('Car #', i) racer_list.append(car) print(car) #create a list attribute called racer_list #for each number from 0 up to racers (the total number of racers) #create a car named Car # (with the number) #add the car to the list #print the car details def getWinner(self): winner = 'No winner' for racer in racer_list: #create a variable called winner and set it equal to 'No winner' #loop through all racers in the list of racers #check if its max distance is greater than the race distance #if the current winner is 'No winner' set this to the winner #otherwise, check if the speed of this racer is faster than winner.speed #update the current winner accordingly #after finishing the loop, return the winner def main(): #create a new race indy = Race() #start a new race with 18 racers #print the results of the race by calling getWinner() if __name__=="__main__": main()
Discover more documents: Sign up today!
Unlock a world of knowledge! Explore tailored content for a richer learning experience. Here's what you'll get:
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help