# Name: James Yang # VUnetID: yangjh1 # Email: james.h.yang@vanderbilt.edu # Class: CS 1104- Vanderbilt University # Section: 1 # Date: 09/21/2020 # Honor Statement: I attest that I understand the honor code for this class and have neither # given nor received any unauthorized aid on this assignment. # Program description: This program calculates the cost of driving given a specific number of miles and a particular gas price. def calc_cost(miles, miles_per_gallon, dollars_per_gallon): """ Calculates the cost of driving for a specific number of miles based on a car's fuel economy. Parameters: miles: The number of miles to calculate. miles_per_gallon: The number of miles a car can travel per gallon. dollars_per_gallon: Cost of fuel per gallon. Returns: The driving cost of a car. """ # Start of statements for the calc_cost function. # Computes the cost per mile driven based on user input. cost_per_mile = dollars_per_gallon / miles_per_gallon # Returns the total cost for the number of miles driven. return cost_per_mile * miles def print_cost(miles, cost): """ Prints the total cost for the number of miles driven. Parameters: miles: The number of miles to calculate. cost: The total cost to calculate. """ # Start of statements for the print_cost function. print("The cost for", miles, "miles is ${:.2f}".format(cost)) def main(): # Add calls to the functions that you defined here. # User inputs are entered miles_per_gallon = float(input()) dollars_per_gallon = float(input()) print("Enter car's miles per gallon:", miles_per_gallon) print("Enter cost of gas per gallon:", dollars_per_gallon) # Total cost is calculated under different numbers of miles. miles = 10 cost = calc_cost(miles, miles_per_gallon, dollars_per_gallon) print_cost(miles, cost) miles = 50 cost = calc_cost(miles, miles_per_gallon, dollars_per_gallon) print_cost(miles, cost) miles = 400 cost = calc_cost(miles, miles_per_gallon, dollars_per_gallon) print_cost(miles, cost) # Remember, statements must be indented once to be part of this function. # DO NOT CHANGE ANYTHING BELOW THIS COMMENT. if __name__ == '__main__': main()
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