Question

In: Computer Science

This problem requires you to prompt the user for some information, and apply simple arithmetic operation...

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.

  • Your job is to prompt the driver for
    • miles per gallon and
    • gas price (in dollars) per gallon

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)

Solutions

Expert Solution

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:


Related Solutions

write a script named compute.sh that is used to do simple arithmetic for the user. There...
write a script named compute.sh that is used to do simple arithmetic for the user. There should be no command line arguments. Instead, all values from the user should be prompted for and read in to the script using the read command. Specifically, you need to ask the user for two integers and an operation string. The operation should be "add", "sub", "mul", "div", or "exp", for addition, subtraction, multiplication, division, and exponentiation, respectively. Your script should take the two...
Problem 4: Reading Binary You must write a program that willcontinuously prompt the user for...
Problem 4: Reading Binary You must write a program that will continuously prompt the user for a string representing a binary number. Then you should validate whether that string is valid binary number, and in case it is you must print the corresponding number in decimal base. Otherwise, you should state that the input string is not a valid binary number. Use the string “QUIT” to end the program. You should be aware that the input string may contain noise...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
Create a program that will prompt for user information for a Web site. Use a structure...
Create a program that will prompt for user information for a Web site. Use a structure to store the data. The structure will need to include the following items: First Name - string Last Name - string Full Name - string Birth Date   - int (assume format yyyymmdd) IsLeasingAutomobile - bool yearlySalary - float Create a single structure from the items listed above. Prompt for all items except "Full Name" and load the input directly into a variable based on...
Simulate a problem with undo. You will need to be connected as user SYSTEM or some...
Simulate a problem with undo. You will need to be connected as user SYSTEM or some other user to whom you have granted the DBA role. . Create an undo tablespace with a single datafile of only 200 KB. Do not set the file to autoextend. . Set the database to use this small undo tablespace. Confirm that an undo segment has been created in this tablespace and that it is online by querying the views DBA_ROLLBACK_SEGS and V$ROLLSTAT. Why...
In this problem you will apply the utility maximization framework to try to understand some of...
In this problem you will apply the utility maximization framework to try to understand some of the effects of the current banking crisis on consumers wellbeing. Take a two period model in which consumers utility function is U(C1,C2)=C1C2 where C1 is consumption in period 1 and C2 consumption in period 2, both retrieved in cash. The consumer is free to borrow or save in period one as the interest rate r, through the banking system. a) Write the consumer's inter-temporal...
You must prompt the user to enter a menu selection. The menu will have the following...
You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line...
(b) You will write a program that will do the following: prompt the user enter characters...
(b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘Q’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘Q’. Example input mJ0*5/]+x1@3qcxQ The ‘Q’ should be included when computing the statistics properties of the input. Since characters are integers...
C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...
C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT