Question

The base fare (B) covers the first mile or less, and only the distance travelled after...

The base fare (B) covers the first mile or less, and only the distance travelled after one mile is charged. For example, if the distance travelled is D = 6.5 miles, then the total fare is given as T = B + P * (6.5 – 1). Use Python to write a program to take as input the prevailing rate P and the distance travelled D, and output the total fare (T) for the given trip. For this program you should assume that the base fare (B) is £4.70.

The program should check that the input value for the prevailing rate (P) is positive and, if not, give the error message ‘The prevailing rate must be positive’. It should also check that the distance travelled (D) is positive and, if not, give the error message ‘Distance travelled should be positive’. In both cases, the user should then be prompted to input the value again until it is positive.

Create and test your program in the following Python environment. You may include comments in the program to make it clearer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We have to keep taking the input from the user until we get a positive value for both the prevailing rate (P) and the distance travelled (D). We can use a while loop and a flag variable for this operation.

If we get both inputs to be positive, we set the value of flag to be False and we break out of the while loop.

Now, we store the values of P and D in float variables. We have been given the value of the base fare B as 4.70.

Now, it's simply a matter of plugging in the values into the equation and get the result.

Here's the code.


flag = True;

while(flag == True):
P = float(input('Enter the prevailing rate : \n'))
  
if(P < 0):
print('The prevailing rate must be positive')
continue
  
D = float(input('Enter the distance travelled : \n'))
  
if(D < 0):
print('The distance travelled must be positive.')
continue
  
flag = False

base_fare = 4.7

total_fare = base_fare + P * (D - 1)

print('The total fare for the entire trip is ' , total_fare)

Also attached are the screenshots of the code and the output for the given input.

flag = True; while(flag == True): P = float(input(Enter the prevailing rate : \n)) if(P < ): print(The prevailing rate mus

Input 6.5 Output Enter the prevailing rate : Enter the distance travelled : The total fare for the entire trip is 37.7

Let me know if you have any doubts in the comments. Please upvote if the answer helped you.

Add a comment
Know the answer?
Add Answer to:
The base fare (B) covers the first mile or less, and only the distance travelled after...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Please. a)Let a program store the result of applying the eval function to the first...

    Python Please. a)Let a program store the result of applying the eval function to the first command-line argument. Print out the resulting object and its type. Run the program with different input: an integer, a real number, a list, and a tuple. (On Unix systems you need to surround the tuple expressions in quotes on the command line to avoid error message from the Unix shell.) Try the string "this is a string" as a commandline argument. Why does this...

  • Please can someone help me to finish this program? It is a C programming not a...

    Please can someone help me to finish this program? It is a C programming not a C++, when programming the program in linux I keep getting expected expression before || token, also please check if everything else is fine, maybe there is something else worng in my program and may ask me later to fix it, I am concern about the sin and cosine. The program is supposed to do all this: 1.Display a welcome message. 2.Prompt for the height...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • Use program control statements in the following exercises: Question 1 . Write pseudocode for the following:...

    Use program control statements in the following exercises: Question 1 . Write pseudocode for the following: • Input a time in seconds. • Convert this time to hours, minutes, and seconds and print the result as shown in the following example: 2 300 seconds converts to 0 hours, 38 minutes, 20 seconds. Question 2. The voting for a company chairperson is recorded by entering the numbers 1 to 5 at the keyboard, depending on which of the five candidates secured...

  • /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid...

    /*************************************************** Name: Date: Homework #7 Program name: HexUtilitySOLUTION Program description: Accepts hexadecimal numbers as input. Valid input examples: F00D, 000a, 1010, FFFF, Goodbye, BYE Enter BYE (case insensitive) to exit the program. ****************************************************/ import java.util.Scanner; public class HexUtilitySOLUTION { public static void main(String[] args) { // Maximum length of input string final byte INPUT_LENGTH = 4; String userInput = ""; // Initialize to null string Scanner input = new Scanner(System.in); // Process the inputs until BYE is entered do {...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Assignment Overview This programming assignment is intended to demonstrate your knowledge of the following:  Writing...

    Assignment Overview This programming assignment is intended to demonstrate your knowledge of the following:  Writing a while loop  Writing a for loop  Writing a while loop with a sentinel value Chocolate Coupons Foothill Fro-cho, LLC, gives customers a coupon every time they purchase a chocolate bar. After they earn a certain number of coupons, they qualify for a free chocolate bar, which they may use toward the purchase of a single chocolate bar. Usually, 7 is the...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT