473,386 Members | 2,042 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,386 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 1524
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...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.