def main():


        print ('Welcome to the mpg calculator program')


    print()
    #get the miles driven using the input function
    miles_driven = input ("Please enter miles driven: ")
    miles_driven = float (miles_driven)
    #get the gallons used using the input function
    gallons_used = input ("Please enter gallons used: ")
    galons_used = float (gallons_used)
    #calculate mpg here, using the inputted values
    mpg = miles_driven / gallons_used
    print('The MPG based on', format(miles_driven, '.2f'),\
          'miles driven and with', format(gallons_used, '.2f'), \
          'gallons used is:', format(MPG, '.2f'),sep=' ')
    #calls main
main()

>>> 
Welcome to the mpg calculator program

Please enter miles driven: 200
Please enter gallons used: 50
Traceback (most recent call last):
  File "C:\Users\Mark\Desktop\ITP 100\mpg.py", line 16, in <module>


        main()


  File "C:\Users\Mark\Desktop\ITP 100\mpg.py", line 11, in main


        mpg = (miles_driven / gallons_used)


TypeError: unsupported operand type(s) for /: 'float' and 'str'

galons_used and gallons_used are 2 different variables. Typo.

Ohhhh....I would never have noticed that, thank you!

(So embarrassing...)

I cleaned it up a little for you:

def main():

    print ('Welcome to the mpg calculator program: \n')

    # get the miles driven using the input function
    # input() gives a string so convert with float()
    miles_driven = float(input ("Please enter miles driven: "))
    # get the gallons used using the input function
    gallons_used = float(input ("Please enter gallons used: "))
    # calculate mpg here, using the inputted values
    mpg = miles_driven / gallons_used
    sf = """\
The MPG based on {:.2f} miles driven and with {:.2f} gallons used is: {:.2f}
    """
    print(sf.format(miles_driven, gallons_used, mpg))

# calls main
main()
def inputt(input_string):
    while True:
        outputt = raw_input(input_string):
        Try:
            float(outputt) == float(abs(int(outputt)))
        except ValueError:
            print("Not a valid number. Please enter a valid number.")
        else:
            return float(outputt)


def evaluate(miles_driven, gallons_used):
        return miles_driven / gallons_used

miles_driven = inputt("Please input miles driven: ")
gallons_used = inputt("Please input gallons used: ")
mpg = evaluate(miles_driven, gallons_used)
print("The MPG based on " + miles_driven + " miles driven and " + gallons_used + " gallons used is " + mpg)
        Try:
            float(outputt) == float(abs(int(outputt)))
        except ValueError:
            print("Not a valid number. Please enter a valid number.")
        else:
            return float(outputt)

"try" is not capitalized

This statement does not make sense,
float(outputt) == float(abs(int(outputt)))
and there is no reason to convert to an int and then convert to a float since converting to an int removes the decimal portion of the float, and finally the return should be under the try.

def inputt(input_string):
    while True:
        outputt = raw_input(input_string):
        try:
            return float(outputt)      
            ## or better
            float_val = flaot(input_string)
            if float_val > 0:  ## won't divide by zero
                return float_val
            else:
                print("Not a valid number.")
            except ValueError:
            print("Not a valid number. Please enter a valid number.")

Whoops, indentdataion and spelling is off in the previous post (ran past 30 minutes-those interruptions!!)

Ohhhh....I would never have noticed that, thank you! (So embarrassing...)

We've all been there.

Woooee,

You still have "flaot" on line 7. :) But thanks for cleaning up my code and adding the divide-by-zero exception evaluation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.