By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

gallons code

P: 64
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
Share this Question
Share on Google+
5 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
Please check my reply in your other thread.

MODERATOR
Dec 20 '08 #2

P: 64
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
Expert Mod 2.5K+
P: 2,851
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

P: 64
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
Expert 100+
P: 469
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

Post your reply

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