473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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! :-)

Expand|Select|Wrap|Line Numbers
  1. numCars = int(input("Enter the number of cars: "))
  2.  
  3. carSpeeds = []
  4.  
  5. for i in range(numCars):
  6.     speed = int(input("Enter the car speed: "))
  7.     carSpeeds.append(speed)
  8.  
  9. carsAboveLimit = 0
  10.  
  11. speedLimit = int(input("Enter the speed limit: "))
  12.  
  13. for i in range(carSpeeds):
  14.     if speed > speedLimit
  15.     carsAboveLimit =+ 1
  16.     i = i +1
  17.  
  18. percent = int(carsAboveLimit)/len(carSpeeds)
  19.  
  20. print("The percentage of cars over the speed limit is", percent)
  21.  
  22.  
  23.  
  24.  
Feb 9 '15 #1

✓ answered by bvdet

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]

1 1523
bvdet
2,851 Expert Mod 2GB
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]
Feb 9 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul Keating | last post by:
I have a very simple COM server written in Python that is trying to return a two-dimensional array 268 x 20. The values it contains are some small integers, some short (<29 character) Unicode...
0
by: mrwoopey | last post by:
Hi, My OLAP data cube is giving me the following error when I am manipulating OLAP data cube views: "the data being processed is over the allowed limit" I know that this message is caused by...
29
by: Paul | last post by:
Hi, I'd like to limit the number of selections a user can make in a multiple select listbox. I have a note on the interface to say that only x no. of items should be selected and I check the...
5
by: Simon Harvey | last post by:
Hi All, I have a colleague that I wprk with that develops using ASP. I develop using ASP.net. He seems to make sites much faster than me and I am wondering if its because of the two different...
5
by: Matt | last post by:
Does anyone know or care to find out the speed difference between a directcast call and a direct call? for instance, i might have these lines of code: dim j as collections.arraylist ...
0
by: DBLWizard | last post by:
Howdy All, I am trying to find a way to limit the drag & drop in Excel. I am building an Excel application using visual studio 2005 and vsto. I am allowing drag and drop on one of tha pane's...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
8
by: Julien Fiore | last post by:
Hi, I wrote a function to compute openness (a digital elevation model attribute). The function opens the gdal-compatible input raster and reads the values in a square window around each cell. The...
0
by: toka | last post by:
Hello, I use HttpWebRequest/HttpWebResponse to retrieve a file from weberver. I retrieve it as a Stream and write to file. But this simple method uses all available network bandwidth. How can I...
0
by: Kowalski MeM | last post by:
Hi, I'm tring to code a download page. But I have to list the files from multiple ftp accounts. So I took the code php.net to limit the speed and adapted that for ftp. But I have a problem when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.