Find the percent of cars over the speed limit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • depasqualen
    New Member
    • Jan 2015
    • 6

    #1

    Find the percent of cars over the speed limit?

    In this program I am attempting to compute the percentage of cars that exceed the speed limit, I figured out how to create the list of cars, and I made it so the user picks the speed limit, but now I'm unsure how to print out the calculated percentage of cars over the speed limit. I know the equation should probably look something like the number of cars over the speed limit divided by numCars. I'm very new to programming so any form of help would be appreciated! :-)

    Code:
    numCars = int(input("Enter the number of cars: "))
    
    carSpeeds = []
    
    for i in range(numCars):
        speed = int(input("Enter the car speed: "))
        carSpeeds.append(speed)
    
    carsAboveLimit = 0
        
    speedLimit = int(input("Enter the speed limit: "))
    
    for i in range(carSpeeds):
        if speed > speedLimit
        carsAboveLimit =+ 1
        i = i +1
    
    percent = int(carsAboveLimit)/len(carSpeeds)
    
    print("The percentage of cars over the speed limit is", percent)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You are almost there. for i in range(carSpeeds ): won't work because carSpeeds is a list. The easiest way to create the list carsAboveLimit is to use a list comprehension.
    Code:
    carsAboveLimit = [speed for speed in carSpeeds if speed > speedLimit]

    Comment

    Working...