# It is a common practice to initialize variables as you can in the beginning of your code. # These variables initialized in the beginning of your code are called "global variables", # as they can be used everywhere in the program once they are initialized here. miles = 0.0 # variable for miles to travel mpg = 0.0 # variable for miles per gallon gallons = 0.0 # variable for gallons of gas price = 0.0 # variable for price of gas cost = 0.0 # variable for cost of trip # !! Note: Due to the nature of the formula we are using having so much division, as well as !! # # !! the variables potentially being decimal numbers, all of them are initialized as float. !! # # This is the input stage of the program. Prompts and instructions should be given the input(.) # and print(.) functions. Also, input data should be stored in variables using the "=" operator. miles = float(input("How many miles are you traveling? ")) # input prompt for miles to travel print miles # print number of miles mpg = float(input(" How many miles per gallon does your vehicle get? ")) # input prompt for miles for gallon print mpg # print mpg price = float(input(" What is the price of a gallon of gas? ")) # input prompt for miles for gallon print price # print price # !! Note: There is a difference between "float" and "float(.)" ~ "float" is used in initializing !! # # !! a variable and "float(.) is used in converting what ever "." is to a float data type. !! # # This is the actual calculator part of the program. THe math is not too intensive and is based on # what we wrote down in the notes. Please try to follow the practice of figuring out the math first # on paper in future programs. gallons = round(float(miles/mpg)) cost = price*gallons print "Gallons of gas needed:", gallons, "Cost of trip:", cost
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question