Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello. I am working on a python program that is about a car racing game. I was wondering if anyone could help me condense this

Hello. I am working on a python program that is about a car racing game. I was wondering if anyone could help me condense this for loop down so it is not so long and repetitive. Also, with all the if statements, I have to calculate the car engine failure at a certain speed. It gives me a negative one for the value instead of the speed (which is good), however, it goes away with the next loop. I need it to stay in the car speed list all throguhout the for loop. The bolded part is the part I would like to be shorter. If anyone could help or has any ideas it will be much appreciated.

def main():

import random laps = eval(input("Numbers of laps to be covered in the race (>1):")) distance = eval(input("Distance for each lap (>=10 km):")) min_speed = eval(input("The minimum speed for each car (>40km/hr):")) max_speed = eval(input("The maximum speed for each car (>min_speed):")) cars = 10 car_speed_list = [] standings = [] lap_time_list = [] time_list = [] total1 = 0 total2 = 0 total3 = 0 total4 = 0 total5 = 0 total6 = 0 total7 = 0 total8 = 0 total9 = 0 total10 = 0 lap_var = 0 if min_speed>max_speed: print("Error! The maximum speed", max_speed, "smaller than the mimimum speed", min_speed) exit() print(" ")

mid_speed = (max_speed+min_speed)/2 allowed_speed = max_speed-min_speed minimum_failed_speed = mid_speed-(allowed_speed * 0.025) maximum_failed_speed = mid_speed+(allowed_speed * 0.025) print("You have set the speed range:", [min_speed, max_speed]) faliure = print("Engine faliure speed range defined", [minimum_failed_speed, maximum_failed_speed])

print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")

for i in range (laps): lap_var = i+1 print("After lap", lap_var, ":")

car_speed = [random.uniform(min_speed, max_speed) for k in range(10)] if laps > 0: car_speed_list.clear() for b in range (10): car_speed_list.append(round(car_speed[b], 2)) s1 = car_speed_list[0] s2 = car_speed_list[1] s3 = car_speed_list[2] s4 = car_speed_list[3] s5 = car_speed_list[4] s6 = car_speed_list[5] s7 = car_speed_list[6] s8 = car_speed_list[7] s9 = car_speed_list[8] s10 = car_speed_list[9]

if minimum_failed_speed<=car_speed_list[0]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 1 failed at speed:", car_speed_list[0]) car_speed_list[0] = -1 if minimum_failed_speed<=car_speed_list[1]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 2 failed at speed:", car_speed_list[1]) car_speed_list[1] = -1 if minimum_failed_speed<=car_speed_list[2]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 3 failed at speed:", car_speed_list[2]) car_speed_list[2] = -1 if minimum_failed_speed<=car_speed_list[3]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 4 failed at speed:", car_speed_list[3]) car_speed_list[3] = -1 if minimum_failed_speed<=car_speed_list[4]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 5 failed at speed:", car_speed_list[4]) car_speed_list[4] = -1 if minimum_failed_speed<=car_speed_list[5]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 6 failed at speed:", car_speed_list[5]) car_speed_list[5] = -1 if minimum_failed_speed<=car_speed_list[6]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 7 failed at speed:", car_speed_list[6]) car_speed_list[6] = -1 if minimum_failed_speed<=car_speed_list[7]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 8 failed at speed:", car_speed_list[7]) car_speed_list[7] = -1 if minimum_failed_speed<=car_speed_list[8]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 9 failed at speed:", car_speed_list[8]) car_speed_list[8] = -1 if minimum_failed_speed<=car_speed_list[9]<=maximum_failed_speed: if lap_var%2 == 0: print("Car 10 failed at speed:", car_speed_list[9]) car_speed_list[9] = -1

print("Car speed:", car_speed_list)

t1 = (distance/s1)*60 total1 = total1 + t1 t2 = (distance/s2)*60 total2 = total2 + t2 t3 = (distance/s3)*60 total3 = total3 + t3 t4 = (distance/s4)*60 total4 = total4 + t4 t5 = (distance/s5)*60 total5 = total5 + t5 t6 = (distance/s6)*60 total6 = total6 + t6 t7 = (distance/s7)*60 total7 = total7 + t7 t8 = (distance/s8)*60 total8 = total8 + t8 t9 = (distance/s9)*60 total9 = total9 + t9 t10 = (distance/s10)*60 total10 = total10 + t10 time_list = [total1, total2, total3, total4, total5, total6, total7, total8, total9, total10] for c in range(10): time_list[c]= round(time_list[c],2) print(time_list)

lap_copy = time_list.copy() lap_copy.sort() c1 = time_list.index(lap_copy[0]) c2 = time_list.index(lap_copy[1]) c3 = time_list.index(lap_copy[2]) c4 = time_list.index(lap_copy[3]) c5 = time_list.index(lap_copy[4]) c6 = time_list.index(lap_copy[5]) c7 = time_list.index(lap_copy[6]) c8 = time_list.index(lap_copy[7]) c9 = time_list.index(lap_copy[8]) c10 = time_list.index(lap_copy[9])

standings = [c1 + 1, c2 + 1, c3+ 1, c4 + 1, c5 +1 , c6 + 1, c7 + 1, c8 + 1, c9 + 1, c10 + 1] print(standings) print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ")

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students also viewed these Databases questions

Question

4. What decision would you make and why?

Answered: 1 week ago

Question

3. Review the evidence. Do you believe the testimony presented?

Answered: 1 week ago