448,867 Members | 1,154 Online
Need help? Post your question and get tips & solutions from a community of 448,867 IT Pros & Developers. It's quick & easy.

# Find the percent of cars over the speed limit?

 P: 6 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! :-) Expand|Select|Wrap|Line Numbers 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)         Feb 9 '15 #1

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.
Expand|Select|Wrap|Line Numbers
1. carsAboveLimit = [speed for speed in carSpeeds if speed > speedLimit]

 Expert Mod 2.5K+ P: 2,851 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. Expand|Select|Wrap|Line Numbers carsAboveLimit = [speed for speed in carSpeeds if speed > speedLimit] Feb 9 '15 #2