473,320 Members | 1,828 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,320 software developers and data experts.

gallons code

here i have made code, how could i imporve this version of my code, and make it more effeicnt. below is the code.

i have Write a program that asks the user to input
• The capacity in gallons of a fuel tank
• The fuel efficiency in miles per gallon
• The price of a gallon of petrol
Then outputs:
• how far the car can go with the gas in the tank (capacity x mpg)
• and the cost per 100 miles. (100/mpg*price)
i have to also Ensure the output is in integer format, to a precision of 5.

Expand|Select|Wrap|Line Numbers
  1. print "--------Welcome To Fuel Calculator Program--------"
  2.  
  3. capacity = input ("\nPlease enter the capacity of the fuel tank in gallons: ")
  4.  
  5. mpg = input ("Please enter the fuel efficiency in MPG: ")
  6.  
  7. pricepergallon = input ("Please enter the price per gallon of petrol? £: ")
  8.  
  9. carcango = capacity * mpg
  10.  
  11. print "\n\nOn a full tank, the car can travel:","\t%.5d" % carcango, "miles"
  12.  
  13. print "The cost of travelling 100 miles is: \t£""%.5d" % (100.0/mpg *pricepergallon)
  14.  
  15. print "\n--------End Program--------"
Dec 20 '08 #1
5 6225
Curtis Rutland
3,256 Expert 2GB
Please check my reply in your other thread.

MODERATOR
Dec 20 '08 #2
but look, if you can see, i have try to make code for this question, i just want your help, to tell, me how i could imporve my verion of code.
Dec 20 '08 #3
bvdet
2,851 Expert Mod 2GB
Use raw_input() instead of input(). Function input() evaluates the entered string and is typically not recommended for this type of application. Since raw_input() always returns a string, you will need to convert to a number, probably with float().

The pound sign can be represented by "\xa3". Python does not like literal unicode characters such as "£".

I have reformatted your print statements. To format an integer, use %d. To format a float to 2 decimal places, use %.2f.

Expand|Select|Wrap|Line Numbers
  1. print "--------Welcome To Fuel Calculator Program--------"
  2.  
  3. capacity = float(raw_input ("Please enter the capacity of the fuel tank in gallons: "))
  4.  
  5. mpg = float(raw_input ("Please enter the fuel efficiency in MPG: "))
  6.  
  7. pricepergallon = float(raw_input ("Please enter the price per gallon of petrol (\xa3)"))
  8.  
  9. carcango = capacity * mpg
  10.  
  11. print "\n\nOn a full tank, the car can travel:\t%d miles" % carcango
  12.  
  13. print "The cost of travelling 100 miles is: \t\xa3%.2f" % (100.0 / mpg * pricepergallon)
  14.  
  15. print "\n--------End Program--------"
HTH
-BV
Dec 21 '08 #4
i made the changes you told me to make, but now the programe does, not work, as an there is error message, which says " capacity = float (raw_imput("\nPlease enter the capacity of the fuel tank in gallons: "))
NameError: name 'raw_imput' is not defined"

below is the code;
Expand|Select|Wrap|Line Numbers
  1. print "--------Welcome To Fuel Calculator Program--------"
  2.  
  3. capacity = float (raw_imput("\nPlease enter the capacity of the fuel tank in gallons: "))
  4.  
  5. mpg = float(raw_imput("Please enter the fuel efficiency in MPG: "))
  6.  
  7. pricepergallon = float(raw_input("Please enter the price per gallon of petrol? £: "))
  8.  
  9. carcango = capacity * mpg
  10.  
  11. print "\n\nOn a full tank, the car can travel:","\t%.5d" % carcango, "miles"
  12.  
  13. print "The cost of travelling 100 miles is: \t\xa3%.2f" % (100.0/mpg *pricepergallon)
  14.  
  15. print "\n--------End Program--------"
Dec 21 '08 #5
boxfish
469 Expert 256MB
Are you sure you can't understand this error message? It says it doesn't know what you mean by "raw_imput". It's just a spelling mistake.
Dec 21 '08 #6

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
3
by: imran akhtar | last post by:
i have to Write a program that asks the user to input • The capacity in gallons of a fuel tank • The fuel efficiency in miles per gallon • The price of a gallon of petrol below is the code that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.