In: Computer Science
This problem requires you to prompt the user for some information, and apply simple arithmetic operation to generate an output.
We all know that driving is expensive. So let's write a program to observe this.
You should prompt the user with the words: Enter miles per gallon: at which time the users enters a number, and then Enter the gas price:at which time the user enters the second number. The prompts must be EXACTLY as written, including the colon (:) character, or your test cases will fail. Since you are prompting the user for input, you should not enter anything into the (optional) input box below, but input your numbers after your prompts.
Both inputs should be read as float data type , and your program will output the gas cost for 10 miles, 50 miles, and 400 miles. Due to the way floating point numbers are internally stored, there can be some discrepancies with the precision of calculations depending on the order things are done. To prevent this from causing your submission to be marked incorrect, please calculate the cost in the following order: The miles increment (10, 50, or 400) multiplied by 1.0/miles per gallon multiplied by the gas price.
Example: If the input is:
Enter miles per gallon:20.0 Enter the gas price:3.1599
Then the output is:
1.57995 7.89975 63.198
Note: Real per-mile cost would also include maintenance and depreciation.
Someone please help! Can't get it right! (This is python)
PROGRAM:
miles_per_gallon=float(input("Enter miles per gallon:"))
# [Get the miles per gallon as input from the user as floating point number]
gas_price=float(input("Enter the gas price:"))
#[ Similarly get the gas price from the user as floating point
number]
print(10*1.0/miles_per_gallon*gas_price,end=' ')
# [The gas cost for 10 miles is given by 10 multiplied with 1.0
divided by miles per gallon which is then multiplied with the gas
price( as given in the question). end=' ' is used to prevent the
program from going to the next line and providing only a space so
that all the 3 outputs are displayed in the same line separated
only by spaces ]
print(50*1.0/miles_per_gallon*gas_price,end=' ')
#[Similarly print the gas cost for 50 miles and 400 miles using
the formula given]
print(400*1.0/miles_per_gallon*gas_price,end=' ')
OUTPUT: