gallons code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    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.

    Code:
    print "--------Welcome To Fuel Calculator Program--------"
    
    capacity = input ("\nPlease enter the capacity of the fuel tank in gallons: ")
    
    mpg = input ("Please enter the fuel efficiency in MPG: ")
    
    pricepergallon = input ("Please enter the price per gallon of petrol? £: ")
    
    carcango = capacity * mpg
    
    print "\n\nOn a full tank, the car can travel:","\t%.5d" % carcango, "miles"
    
    print "The cost of travelling 100 miles is: \t£""%.5d" % (100.0/mpg *pricepergallon)
    
    print "\n--------End Program--------"
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please check my reply in your other thread.

    MODERATOR

    Comment

    • imran akhtar
      New Member
      • Nov 2008
      • 64

      #3
      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.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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.

        [code=Python]print "--------Welcome To Fuel Calculator Program--------"

        capacity = float(raw_input ("Please enter the capacity of the fuel tank in gallons: "))

        mpg = float(raw_input ("Please enter the fuel efficiency in MPG: "))

        pricepergallon = float(raw_input ("Please enter the price per gallon of petrol (\xa3)"))

        carcango = capacity * mpg

        print "\n\nOn a full tank, the car can travel:\t%d miles" % carcango

        print "The cost of travelling 100 miles is: \t\xa3%.2f" % (100.0 / mpg * pricepergallon)

        print "\n--------End Program--------"[/code]

        HTH
        -BV

        Comment

        • imran akhtar
          New Member
          • Nov 2008
          • 64

          #5
          code

          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("\nP lease enter the capacity of the fuel tank in gallons: "))
          NameError: name 'raw_imput' is not defined"

          below is the code;
          Code:
          print "--------Welcome To Fuel Calculator Program--------"
          
          capacity = float (raw_imput("\nPlease enter the capacity of the fuel tank in gallons: "))
          
          mpg = float(raw_imput("Please enter the fuel efficiency in MPG: "))
          
          pricepergallon = float(raw_input("Please enter the price per gallon of petrol? £: "))
          
          carcango = capacity * mpg
          
          print "\n\nOn a full tank, the car can travel:","\t%.5d" % carcango, "miles"
          
          print "The cost of travelling 100 miles is: \t\xa3%.2f" % (100.0/mpg *pricepergallon)
          
          print "\n--------End Program--------"

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            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.

            Comment

            Working...