Answer:
The solution code is written in Python.
<u>First version (user input through terminal)</u>
- food_price = float(input("Enter food price: "))
- sales_tax = food_price * 0.07
- tips = food_price * 0.15
- total_cost = food_price + sales_tax + tips
- print("Food price: $" + str(food_price))
- print("Sale tax: $" + str(sales_tax))
- print("Tips: $" + str(tips))
- print("Total cost: $" + str(total_cost))
- print("\n")
<u>Second version (input from text file)</u>
- with open("text.txt") as file:
- data = file.readlines()
- for r in data:
- food_price = float(r)
- sales_tax = food_price * 0.07
- tips = food_price * 0.15
- total_cost = food_price + sales_tax + tips
- print("Food price: $" + str(food_price))
- print("Sale tax: $" + str(sales_tax))
- print("Tips: $" + str(tips))
- print("Total cost: $" + str(total_cost))
- print("\n")
Explanation:
<u>First version</u>
In the first version of code, we can create all the necessary variables to hold the value of food price, sales tax, tips and total cost (Line 1 -4). Please note we get the food price using the input() function to prompt user to enter the value through terminal (Line 1).
Next display the values using print() function (Line 5 - 8)
<u>Second version</u>
The second version of code is similar to the first version except that we need to change the code to accept input from a text file (Line 1 -2) instead of using the input function.
We presume there might be multiple values in the text file and therefore it is recommended to use readlines() method to get the value line by line from the text file (Line 2) and use for loop to traverse through each row of food price value and calculate the sales tax, tips and total cost (Line 4-7).