Lab 2 cs111
Lab 2 cs111
Lab 2 cs111
Lab 2: Modules
This lab accompanies Chapter 3 of Starting Out with Programming Logic & Design. Name Tra!is Smith" #amadou Diallo$$$$$$$$$$$$$$$$$$$$$$$$$$$
Step 2: +i!en a total sales of ,-."/0." answer the following 1hat is the calculated state ta*2 1/33.33 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 1hat is the calculated count& ta*2 $$451.05$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 1hat is the calculated total ta*2 $$$$$$$16-4.3-$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Step 1 This program is most easil& sol!ed using 7ust four !ariables. Declare the !ariables that &ou will need in the program" using the proper data t&pe and documenting the purpose. Variable Name
'eclare )eal total*ales 'eclare )eal countyTax 'eclare )eal stateTax 'eclare )eal totalTax
Purpose
*tores total sales the inputs *tores the calculated tax *tores the calculated tax *tores the calculated tax user county state total
Step 2: +i!en the ma7or tas' in!ol!ed in this program" what modules might &ou consider including2 Describe the purpose of each module. 8(eference Defining and Calling a #odule" page .39. Module Name
"odule input'ata () "odule () "odule "odule "odule calc,ounty calc*tate () calcTotal () print'ata () ,alculates state tax at 4- of monthly sales ,alculates total amount of monthly
Purpose
Allows the user to enter re+uired user input ,alculates county tax at 2- of monthly sales
Step 3: Complete the pseudocode b& writing the missing lines. 8(eference Defining and Calling a #odule" page .3:319. ;lso" when writing &our modules and ma'ing calls" be sure to pass necessar& !ariables as arguments and accept them as reference parameters if the& need to be modified in the module. 8(eference Passing ;rguments b& <alue and b& (eference" page 0. = 1/39.
"odule main () //'eclare local 0aria#les 'eclare )eal total*ales 11 'eclare )eal countyTax 'eclare )eal stateTax 'eclare )eal totalTax 1111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 //2unction calls ,all input'ata(total*ales) ,all calc,ounty(total*ales( countyTax) 1 ,all calc*tate(total*ales) ,all calcTotal(countyTax( stateTax( totalTax) ,all print'ata(countyTax( stateTax( totalTax 11111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 3nd "odule //this module takes in the re+uired user input "odule input'ata()eal )ef total*ales) 'isplay 43nter the total sales for the month.5 6nput total*ales 3nd "odule //this module calculates county tax //total*ales can #e a 0alue parameter #ecause it is not //changed in the module. //countyTax must #e a reference parameter #ecause it is //changed in the module "odule calc,ounty()eal total*ales( )eal )ef countyTax) countyTax 7 total*ales 8 .$2 3nd "odule //this module calculates state tax "odule 1 "odule calc*tate()eal total*ales( )eal )ef stateTax) stateTax 7 total*ales 8 .$4
111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 3nd "odule //this module calculates total tax "odule 11111 "odule calcTotal()eal countyTax( )eal stateTax( )eal )ef totalTax) totalTax 7 countyTax 9 stateTax 11111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 3nd "odule //this module prints the total( county( and state tax "odule 1 "odule print'ata()eal countyTax( )eal stateTax( )eal totalTax) 111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111 3nd "odule
Step 1: Start (aptor and sa!e &our document as Lab 2-3. The .rap file e*tension will be added automaticall&. Start b& adding a Comment bo* that declares &our !ariables. ?ere is a start to how &our Comment bo* should loo'.
Step 2: The ne*t step in &our flowchart should be to call &our methods. Clic' the Call S&mbol on the Left and Drag and Drop to the flow lines between Start and Stop. Double clic' on the Call S&mbol and t&pe the name of &our first method. @or e*ample" t&pe inputData in the )nter Call bo*. Do not put the 8 9 when using (aptor. Clic' the Done button. ; new bo* will pop up that will as' &ou to create a new tab. Clic' Aes. ; new tab will be created for &our new method. Notice the new Tab called inputData.
Starting Out with Programming Logic and Design Step 3: Continue this process to add &our additional methods" which are calcCount&89 calcState89" calcTotal89 and printData89. Step ": Clic' on the inputData tab and add the necessar& code as identified in &our pseudocode in Lab 2.2. Bn (aptor" there is no need to pass !ariables as (eferences as in pseudocode. Aour inputData method might loo' li'e the following
Step #: Continue to code the remaining methods" which are calcCount&89 calcState89" calcTotal89 and printData89. Bf &ou happened to e*ecute &our program without completing &our program" an error will occur such as
Aour calculations methods input bo* might loo' li'e the following
Aour output data methods bo* might loo' li'e the following
Step $: ;fter &our program is complete" clic' (un" then )*ecute to @inish. @or &our input" enter 6.345 as &our total monthl& sales. Bf &our program is coded correctl&" the output should be as follows The county tax is $ 3!".#$## The state sa%es tax is $2" &. '## The tota% tax is $ &#" .2&## ----(un )inishe*---Step %: The final step is to insert &our finished flowchart in the space below. Bnside (aptor" select @ile and the Print to Clipboard from the menu. Bnside 1ord in the space below" select )dit and Paste. Aou will ha!e to do this for each module &ou created. PASTE FLOWCHART HERE
Step 3: ;fter &our documentation" add the following function definition and function call.
:This function is to welcome people to my program def welcome1message(): print ;Welcome to my program using functions; print ;"y name is <oe *tudent; :This is a function call welcome1message()
Step ": Clic' (un" then (un #odule to see &our output. Bt should loo' li'e the following
6'=3 .2. >>> 7777777777777777777 )3*TA)T 777777777777777777777 >>> Welcome to my program using functions "y name is <oe *tudent >>>
Step #: Change &our program so that the function call is tabbed o!er" such as
:This function is to welcome people to my program def welcome1message(): print ;Welcome to my program using functions; print ;"y name is <oe *tudent; :This is a function call welcome1message() #tab this line over
Step $: Clic' (un and (un #odule again. AouDll notice that nothing is printed. This is because in P&thon" each line in a bloc' must be indented and aligned. @unction calls must be flushed to the left" and each line within a module must be aligned e!enl&. The following will cause a s&nta* error.
def my1function(): print ;And now for; print ;something completely; print ;different.;
1/
Step %: Since programs normall& center around a main function" modif& &our program so that it loo's as follows
:The main function def main(): welcome1message()
:This function is to welcome people to my program def welcome1message(): print ;Welcome to my program using functions; print ;"y name is <oe *tudent; :This is the main function that starts the program in :motion main() :calls main
Step (: ;dd an additional function to &our program that is called goodb&e$message8 9. The contents of this function should print a goodb&e message. )*ecute &our program so that it wor's and paste the final code below PASTE CODE HERE
)Student Name )*ate )*escription: +his program tests out the di,,erent !a&s to use ,unctions )+he main ,unction de, main-.: !elcome/message-. )causes !elcome/message to e0ecute goodb&e/message-. )causes goodb&e/message to e0ecute )+his ,unction is to !elcome people to m& program de, !elcome/message-.: print 12elcome to m& program using ,unctions1 print 1M& name is 3oe Student1 de, goodb&e/message-.:
11
print 14oodb&e...than5 &ou ,or using m& program1 )+his is the main ,unction that starts the program in motion main-.
1-
Step 3: ;dd a function called main8 9 and a function call to main. Aour code might loo' li'e this
:This program uses functions and 0aria#les :the main function def main(): print ;Welcome to the tip calculator program; print :prints a #lank line :calls main main()
Step ": ;dd a function called inputName89 under the def main89 function. Aour code might loo' as follows
:this function inputs name def input?ame()
Step #: >nder &our function definition" write a statement that allows the user to enter their name. Bnside of the main function" call inputName89 and write a print statement that displa&s the name. Aour code might loo' as follows
:This program uses functions and 0aria#les :the main function def main(): print ;Welcome to the 0aria#le program; print :prints a #lank line input?ame() print ;@ello;( name :this function inputs name def input?ame(): name 7 raw1input(;3nter your name: ;) :calls main main()
13
Step $: Compile and run &our program. Notice that when the program attempts to displa& the name" a s&nta* error occurs. This is because name is declared as a local !ariable within the inputName8 9 function and main cannot access it. Step %: There are a couple of wa&s to fi* this error. )*amine the following code
:This program uses functions and 0aria#les :the main function def main(): print ;Welcome to the 0aria#le program; print :prints a #lank line name 7 input?ame() print ;@ello;( name :this function inputs data def input?ame(): name 7 raw1input(;3nter your name: ;) return name :calls main main()
The local !ariable name is declared in main and set e%ual to whate!er the inputName8 9 function returns. Notice the return name that is at the end of the inputName8 9 function. This passes the !alue that was ta'en in bac' to main. Step (: ;dd an additional function to &our program that is called input;ge8 9. The contents of this function should be structured similar to the inputName8 9 function e*cepts that it as's the user to enter their age. ;dditionall&" ma'e a call to this new function such as age E input;ge8 9. Aou should also displa& the !alue of age after the name is displa&ed. )*ecute &our program so that it wor's and paste the final code below PASTE CODE HERE
)+his program uses ,unctions and 6ariables )the main ,unction de, main-.: print 12elcome to the tip calulator program1 print )prints a blan5 line name7inputName-.
15
age7inputAge-. print18ello19name print 1:our age is19age )this ,unction inputs name de, inputName-.: name 7 ra!/input-1;nter &our name:1. return name )this ,unctions inputs age de, inputAge-.: age 7 input-1;nter &our age:1. return age )calls main main-.
14
Step 1: Start the BDL) )n!ironment for P&thon. Prior to entering code" sa!e &our file b& clic'ing on @ile and then Sa!e. Select &our location and sa!e this file as Lab2-'.py. Ce sure to include the .p& e*tension. Step 2: Document the first few lines of &our program to include &our name" the date" and a brief description of what the program does. Description of the program should be
Write a program that will calculate a 2$- tip and a Atax 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.
Step 3: ;dd a function called main8 9 and a function call to main. Step ": ;dd the function definition for input$meal8 9" calc$tip8 9" calc$ta*8 9" calc$total89" and print$info8 9. Aour code might loo' li'e the following
:This program uses functions and 0aria#les :the main function def main(): print ;Welcome to the meal calculator program; print :prints a #lank line :this function will input meal price def input1meal(): :this function will calculate tip at 2$def calc1tip(): :this function will calculate tax at Adef calc1tax(): :this function will calculate tip( tax( and the total :cost def calc1total(): :this function will print tip( tax( the mealprice( and :the total def print1info(): :calls main
16
Step #: Bnside of main89 under the print :prints a #lank line statement" create a local !ariable named mealprice that is set to the input$meal89 function. This should loo' li'e the following
mealprice 7 input1meal()
Step $: ;dd the following lines of code inside of input$meal89 function. This should loo' li'e the following
mealprice 7 input(;3nter the meal price B;) mealprice 7 float(mealprice) return mealprice
The first line as's the user to enter their meal price. The second line con!erts the !alue to a float" since it will li'el& be a decimal !alue. This must be done with all potential decimal !alues that the user enters. The third line returns the input !alue of mealprice to the place where it was called 8in Step 49. Step %: Bnside of main89 under the meal 7 input1meal() statement" create a local !ariable named tip that is set to the calc$tip89 function. Bn this case" &ou must pass mealprice to the function" so it must be placed between the parentheses. This should loo' li'e the following
tip 7 calc1tip(mealprice)
Step (: ;dd the following lines of code inside of calc$tip8mealprice9 function. The entire function should loo' li'e the following
def calc1tip(mealprice): tip 7 mealprice 8 .2$ return tip
The first line is the function definition. Bt accepts mealprice as a parameter. The second line is to calculate tip as -/F of the mealprice. The third line returns the calculated tip to the place where it is called. Step <: Bnside of main89 under the tip 7 calc1tip(mealprice) statement" create a local !ariable named ta* that is set to the calc$ta*89 function. Bn this case" &ou must pass mealprice to the function" so it must be placed between the parentheses. This should loo' li'e the following
tax 7 calc1tax(mealprice)
Step 1=: ;dd the following lines of code inside of calc$ta*8mealprice9 function. The entire function should loo' li'e the following
1.
The first line is the function definition. Bt accepts mealprice as a parameter. The second line is to calculate ta* as 6F of the mealprice. The third line returns the calculated ta* to the place where it is called. Step 11: Bnside of main89 under the tax 7 calc1tax(mealprice) statement" create a local !ariable named total that is set to the calc$total89 function. Bn this case" &ou must pass mealprice" tip" and ta* to the function" so the& must be placed between the parentheses. This should loo' li'e the following
total 7 calc1total(mealprice( tip( tax)
Step 12: ;dd the following lines of code inside of calc$total8mealprice" tip" ta*9 function. The entire function should loo' li'e the following
def calc1total(mealprice( tip( tax): total 7 mealprice 9 tip 9 tax return total
The first line is the function definition. Bt accepts mealprice" tip" and ta* as parameters. The second line is to calculate the total of all three !alues added together. The third line returns the calculated total to the place where it is called. Step 13: Bnside of main89 under the total 7 calc1total(mealprice( tip( tax) statement" call the print$info 89 function. Bn this case" &ou must pass mealprice" tip" ta*" and total to the function" so the& must be placed between the parentheses. This should loo' li'e the following
print1info(mealprice( tip( tax( total)
Step 1": ;dd the following lines of code inside of print$info8mealprice" tip" ta*" total9 function. The entire function should loo' li'e the following
def print1info(mealprice( tip( tax( total): print ;The meal price is B;( mealprice print ;The tip is B;( tip print ;The tax is B;( tax print ;The total is B;( total
The first line is the function definition. Bt accepts mealprice" tip" ta*" and total as parameters. The following lines print the mealprice" the calculated tip" the calculated ta*" and the calculated total.
13
Step 1#: (un &our module and fi* an& errors &ou ma& ha!e. The most common errors ma& be that &ou ha!e misspelled something when t&ping" or that &our indentations are not aligned properl&. 1hen running &our program" enter -5.4/ as the meal price. Aour output should loo' as follows
Welcome to the tip and tax calculator program 3nter the meal price B24.&$ The meal price is B 24.& The tip is B 4.C The tax is B .4D The total is B %$.ED
Step 1$: 1hen &our program is completed and &ou ha!e tested &our output in Step 14" paste &our completed program below. PASTE CODE HERE
)Student Name )*ate )+his program is designed to calculate the total cost o, a meal )+his program uses ,unctions and 6ariables )the main ,unction de, main-.: print 12elcome to the meal calculator program1 print )prints a blan5 line mealprice 7 input/meal -. tip 7 calc/tip-mealprice. ta0 7 calc/ta0-mealprice. total 7 calc/total-mealprice9 tip9 ta0. print/in,o-mealprice9 tip9 ta09 total. )this ,unction !ill input meal price de, input/meal-.: mealprice 7 input-1;nter the meal price >1. mealprice 7 ,loat-mealprice. return mealprice )this ,unction !ill calculate tip at 2=? de, calc/tip-mealprice.: tip 7 mealprice @ .2= return tip
10
)this ,unction !ill calculate ta0 at $? de, calc/ta0-mealprice.: ta0 7 mealprice @ .=$ return ta0 )this ,unction !ill calculate tip9 ta09 and the total )cost de, calc/total-mealprice9 tip9 ta0.: total 7 mealprice A tip A ta0 return total )this ,unction !ill print tip9 ta09 the mealprice9 and )the total de, print/in,o-mealprice9 tip9 ta09 total.: print 1+he meal price is >19 mealprice print 1+he tip is >19 tip print 1+he ta0 is >19 ta0 print 1+he total is >19 total )calls main main-.
-/
This lab re%uires &ou to translate &our wor' in the pseudocode and flowchart from Lab -.- and Lab -.3 to actual code using P&thon. (ead the following program prior to completing the lab.
A retail company must file a monthly sales tax report listing the total sales for the month and the amount of state and county sales tax collected. The state sales tax rate is 4 percent and the county sales tax rate is 2 percent. Write a program that asks the user to enter the total sales for the month. The application should calculate and display the following: The amount of county sales tax The amount of state sales tax The total sales tax (county plus state)
Consider the following functions for &our program main that calls &our other functions inputData that will as' for the monthl& sales calcCount& that will calculate the count& ta* calcState that will calculate the state ta* calcTotal that will calculate the total ta* printData that will displa& the count& ta*" the state ta*" and the total ta* Bf &our program is correct" sample output might loo' as follows
Welcome to the total tax calculator program 3nter the total sales for the month B 2&AD The county tax is B 2& .%4 The state tax is B &$2.AE The total tax is B D&4.$2
)+his program creates a +a0 report based on the total monthl& sales de, main-.: print 12elcome to the ta0 report program1 print
-1
totalsales 7 input*ata-. count&ta0 7 calc'ount&-totalsales. stateta0 7 calcState-totalsales. totalta0 7 calc+otal-count&ta09 stateta0. print*ata-count&ta09 stateta09 totalta0. )this ,unction !ill input total sales de, input*ata-.: totalsales 7input-1;nter the total sales ,or the month >1. totalsales 7,loat-totalsales. return totalsales )this ,untion !ill calculate the count& ta0 at 2? de, calc'ount&-totalsales.: count&ta0 7 totalsales@ .=2 return count&ta0 )this ,untion !ill cal state ta0 at "? de, calcState-totalsales.: stateta0 7 totalsales@ .=" return stateta0 )this ,unction totals the ta0es de, calc+otal-count&ta09 stateta0.: totalta0 7 count&ta0 A stateta0 return totalta0 )this ,unction prints data de, print*ata-count&ta09 stateta09 totalta0.: print 1+he count& ta0 is >19 count&ta0 print 1+he state ta0 is >19 stateta0 print 1+he total ta0 is >19 totalta0 )calls main main-.