Algorithms
|
|
|
Lab 4: Decisions and Boolean Logic
This lab accompanies Chapter 4 of Gaddis, T. (2016). Starting out with programming logic and design (4th ed.). Boston, MA: Addison-Wesley.
Lab 4.6 – Programming Challenge 1 – Tip, Tax, and Total
Write the Pseudocode for the following programming problem.
Write a program that will calculate a tip based on the meal price and a 6% tax on a meal price. The user will enter the meal price and the program will calculate tip, tax, and the total. The total is the meal price plus the tip plus the tax. Your program will then display the values of tip, tax, and total.
The tip amounts based on the mean price are as follows:
|
Meal Price Range |
Tip Percent |
|
.01 to 5.99 |
10% |
|
6 to 12.00 |
13% |
|
12.01 to 17.00 |
16% |
|
17.01 to 25.00 |
19% |
|
25.01 and more |
22% |
The Pseudocode
TYPE PSEUDOCODE HERE
The Python Code for Reference
#the main function
def main():
print 'Welcome to the tip and tax calculator program'
print #prints a blank line
mealprice = input_meal()
tip = calc_tip(mealprice)
tax = calc_tax(mealprice)
total = calc_total(mealprice, tip, tax)
print_info(mealprice, tip, tax, total)
#this function will input meal price
def input_meal():
mealprice = input('Enter the meal price $')
mealprice = float(mealprice)
return mealprice
#this function will calculate tip at 20%
def calc_tip(mealprice):
if mealprice >= .01 and mealprice <= 5.99:
tip = mealprice * .10
elif mealprice >= 6 and mealprice <=12:
tip = mealprice * .13
elif mealprice >=12.01 and mealprice <=17:
tip = mealprice * .16
elif mealprice >= 17.01 and mealprice <=25:
tip = mealprice * .19
else:
tip = mealprice * .22
return tip
#this function will calculate tax at 6%
def calc_tax(mealprice):
tax = mealprice * .06
return tax
#this function will calculate tip, tax, and the total cost
def calc_total(mealprice, tip, tax):
total = mealprice + tip + tax
return total
#this function will print tip, tax, the mealprice, and the total
def print_info(mealprice, tip, tax, total):
print 'The meal price is $', mealprice
print 'The tip is $', tip
print 'The tax is $', tax
print 'The total is $', total
#calls main
main()