You are on page 1of 18

Starting Out with Programming Logic and Design

Lab 3: Decisions and Boolean Logic


This lab accompanies Chapter 4 of Starting Out with Programming Logic & Design. Name !!!!!!!!!!!!!!!!!!!"dam!!!!!!!!

Lab 3.1 Evaluating Conditions


Critical #e$iew " relational operator determines whether a specific relationship e%ists between two $alues. #elational operators Operator & * &+ *+ ++ .+ Meaning 'reater than Less than 'reater than or e,ual to Less than or e,ual to -,ual to Not e,ual to Boolean Expression (&) (*) ( &+ ) ( *+ ) (++) ( .+ )

/elp 0ideo Double clic1 the file to $iew $ideo

This lab re,uires 2ou to thin1 about possible true and false conditions using if statements. Step 1: Consider the following $alues set to $ariables. m2"ge + 34 2our"ge + 15 m2Number + 51 2ourNumber + 16 $oting"ge + 15 m2Name + 78atie9 2ourName + 7:ob9 Step : :ased on the $alues to the $ariables in Step 1; do the following conditions result in a true or false statement< =#eference :oolean -%pressions; page 11>?. !"e condition
myAge >= yourAge yourAge > myAge myAge == 45 yourAge == votingAge

!rue or #alse True @alse @alse True

Starting Out with Programming Logic and Design


votingAge <= yourAge myAge <= votingAge myName != yourName myNumber <= myAge yourNumber >= myAge yourNumber != 17

4 True @alse True @alse @alse @alse

Step 3: :ased on the $alues to the $ariables in Step 1; what is the e%pected output< /int The output will be either what is printed to the screen; or nothing. =#eference :oolean -%pressions; page 11>?. !"e condition
If myName == yourName Then print We have the ame name! "n# If If myAge >= yourAge Then print I am o$#er or e%ua$ to your age! "n# If If myName != &atie! Then print That i not my name! "n# If If myName == &atie! Then print That i my name! "n# If If myNumber == 17 Then print 'y number i 17! "n# If If myNumber >=() Then print 'y number i () or more! "n# If If yourNumber <= yourAge Then print *our number i $e than or e%ua$ to your age! "n# If If myNumber < yourNumber Then print 'y number i $e ! "n# If If yourAge >= votingAge Then print *ou +an vote! "n# If Af myAge < yourAge Then print I am younger! "n# If

Expected Output Nothing A am older or e,ual to 2our age Nothing That is m2 name Nothing B2 number is 5C or more )our number is less than or e,ual to 2our age Nothing )ou can $ote Nothing

Starting Out with Programming Logic and Design

Lab 3. $seudocode and Decisions


Critical #e$iew Euestions are often as1ed using an if statement such as if59 > *?; whereas the ,uestion as1ed is 7i 9 greater than *!: The general structure of an if statement is If +on#ition Then 4tatement 4tatement "t+. "n# If /elp 0ideo Double clic1 the file to $iew $ideo

This lab re,uires 2ou to thin1 about the steps that ta1e place in a program b2 writing pseudocode. #ead the following program prior to completing the lab.
A retai$ +ompany a ign a ,5))) tore bonu if month$y a$e are ,1))-))) or more. A##itiona$$y- if their a$e e/+ee# 1051 or more of their month$y goa$ of ,2)-)))- then a$$ emp$oyee 3i$$ re+eive a me age tating that they 3i$$ get a #ay off.

Step 1 This program is easiest when sol$ed using Dust one $ariable. Declare the $ariables that 2ou will need in the program; using the proper data t2pe and documenting the purpose. Depending on 2our programming st2le; 2ou ma2 find additional $ariables are useful. Af that is the case; adDust 2our program as necessar2. %ariable &a'e
month$y4a$e

$urpose
4tore the month$y a$e

Step : 'i$en the maDor tas1 in$ol$ed in this program; what modules might 2ou consider including< "lso describe the purpose of the module. Module &a'e
'o#u$e get4a$e 'o#u$e i 7onu 56 56

$urpose
A$$o3 the u er to enter the month$y a$e . Thi mo#u$e 3i$$ #etermine if a bonu hou$# be a3ar#e#. Thi mo#u$e 3i$$ #etermine if a #ay off hou$# be a3ar#e#.

'o#u$e i 8ayoff 56

Starting Out with Programming Logic and Design Step 3: Complete the pseudocode b2 writing the missing lines. Fhen writing 2our modules and ma1ing calls; be sure to pass necessar2 $ariables as arguments and accept them as reference parameters if the2 need to be modified in the module. =#eference Friting a Decision Structure in Pseudocode; page 115?.
'o#u$e main 56 ;;8e+$are $o+a$ variab$e 8e+$are <ea$ month$y4a$e ;;=un+tion +a$$ >a$$ get4a$e 5month$y4a$e 6 >a$$ i 7onu 5month$y4a$e 6 >a$$ i 8ayoff5month$y4a$e 6 "n# 'o#u$e ;;thi mo#u$e ta?e in the re%uire# u er input 'o#u$e get4a$e 5<ea$ <ef month$y4a$e 6 8i p$ay "nter the tota$ a$e for the month.! Input month$y4a$e "n# 'o#u$e ;;thi mo#u$e 3i$$ #etermine if a bonu i a3ar#e# 'o#u$e i 7onu 5<ea$ <ef month$y4a$e 6 If month$y4a$e >=1))))) Then @rint *ou get a bonu of ,5-)))!!!! "n# If "n# 'o#u$e ;;thi mo#u$e 3i$$ #etermine if a$$ emp$oyee get a #ay ;;off. If a$e are greater than or e%ua$ to 1105))- then ;;they get a #ay off. 'o#u$e i 8ayoff 5<ea$ <ef month$y4a$e 6 If month$y4a$e >= 105))) Then @rint *ou get a #ay off!!!! "n# if "n# 'o#u$e

Starting Out with Programming Logic and Design

Lab 3.3 #lo(c"arts


Critical #e$iew The flowchart s2mbol used to indicate some condition is a diamond. "n if statement is called a single alternati$e decision structure. The code will onl2 process if the decision is true.

/elp 0ideo Double clic1 the file to $iew $ideo

This lab re,uires 2ou to con$ert 2our pseudocode in Lab 3.4 to a flowchart. Hse an application such as #aptor or 0isio. Step 1: Start #aptor and sa$e 2our document as Lab 3-3. The .rap file e%tension will be added automaticall2. Start b2 adding a Comment bo% that declares 2our $ariables. /ere is how 2our Comment bo% should loo1.

Step : The ne%t step in 2our flowchart should be to call 2our methods. :elow is a start of how 2our main should loo1.

Starting Out with Programming Logic and Design

Step 3: Continue this process to add 2our additional methods 2ou defined in Lab 3.4; Step 3. Step ): Clic1 on the getSales tab and add the necessar2 code to enter the monthl2 sales. )our getSales method might loo1 li1e the following

Step *: Clic1 on the second module which determines if a bonus of JGCCC is awarded. Clic1 the Selection s2mbol and add it between the start and the end of the module. Double clic1 on the diamond s2mbol and add the code to determine if monthl2Sales is greater than or e,ual to 1CCCCC. The enter selection condition should be written as follows

Starting Out with Programming Logic and Design

Step +: Drag an output s2mbol and drop it on the True line. Double clic1 on the output bo% and add te%t that prints 7)ou earned a JGCCC bonus.9. )our module should li1e as follows

Step ,: #epeat the process in Step I to code 2our ne%t module. Step -: Fhen 2our program is complete; test the following monthl2 sales and ensure that the output matches the following. Af 2our output is different; then re$iew 2our decision statements. Mont"l. Sales monthl2Sales + 1C4GCC monthl2Sales + >CCCC monthl2Sales+ 114GCC Expected Output )ou earned a JGCCC bonus. *nothing& )ou earned a JGCCC bonus. "ll emplo2ees get one da2 off...

Step /: The final step is to insert 2our finished flowchart in the space below. Anside #aptor; select @ile and the Print to Clipboard from the menu. Anside Ford in the space below; select -dit and Paste. )ou will ha$e to do this for each module 2ou created. PASTE FLOWCHART HERE

Starting Out with Programming Logic and Design

Starting Out with Programming Logic and Design

>

Starting Out with Programming Logic and Design

1C

Lab 3.) $.t"on Code


Critical #e$iew An P2thon we use the if statement to write a single alternati$e decision structure. /ere is the general format of the if statement if condition: statement statement etc. @or simplicit2; we will refer to the first line as the if clause. The if clause begins with the word if; followed b2 a condition; which is an e%pression that will be e$aluated as either true or false. " colon appears after the condition. :eginning at the ne%t line is a bloc1 of statements. Fhen the if statement e%ecutes; the condition is tested. Af the condition is true; the statements that appear in the bloc1 following the if clause are e%ecuted. Af the condition is false; the statements in the bloc1 are s1ipped. Fhen strings are e$aluated in a condition; single ,uotation mar1s are used. @or e%ample name ! "#ary" name$ ! "#ar%" if name &! name$: print "'he names are not the same." if name !! (#ary) print ('he name is #ary.)
/elp 0ideo Double clic1 the file to $iew $ideo

Step 1: Start the ADL- -n$ironment for P2thon. Prior to entering code; sa$e 2our file b2 clic1ing on @ile and then Sa$e. Select 2our location and sa$e this file as Lab3-4.py. :e sure to include the .p2 e%tension. Step : Document the first few lines of 2our program to include 2our name; the date; and a brief description of what the program does. Description of the program should be

Starting Out with Programming Logic and Design


AThi program 3i$$ #emon trate ho3 to u e #e+i ion A tatement in @ython.

11

Step 3: Start 2our program with the following code


AThi program #etermine if a bonu hou$# be a3ar#e#

AThe main fun+tion #ef main56B print CWe$+ome to the programC month$y4a$e = get4a$e 56 A get

a$e

AThi fun+tion get the month$y a$e #ef get4a$e 56B month$y4a$e = input5C"nter the month$y month$y4a$e = f$oat5month$y4a$e 6 return month$y4a$e A+a$$ main main56

a$e

,C6

Step ): "dd a function call to the method that determines if a bonus is awarded. The call should be in main and process after month$y4a$e = get4a$e 56. :e sure to pass monthl2Sales to the function as an argument since that will be needed to determine if a bonus is awarded. )our code might loo1 as follows
A=un+tion +a$$ to #etermine bonu i 7onu 5month$y4a$e 6

Step *: Hnder the getSales=? function; code the function that will determine if a bonus is awarded. :e sure to accept monthl2Sales in the parameter list. "lso; note that the if statement is followed b2 a colon; and the print statement inside must be tabbed o$er.
#ef i 7onu 5month$y4a$e 6B if month$y4a$e >= 1)))))B print D*ou have earne# a ,5-))) bonu !!!D

Step +: #epeat the process in Step 4 to ma1e a function call to the method that determines if all emplo2ees get a da2 off. Step ,: #epeat the process in Step G to code the function that will determine if all emplo2ees should get a da2 off. Step -: Clic1 #un and #un Bodule to see how 2our program processes. Test the following monthl2Sales $alues to $erif2 the e%pected output. Mont"l. Sales monthl2Sales + 1C4GCC Expected Output )ou earned a JGCCC bonus.

Starting Out with Programming Logic and Design monthl2Sales + >CCCC monthl2Sales+ 114GCC *nothing& )ou earned a JGCCC bonus. "ll emplo2ees get one da2 off...

14

Step /: -%ecute 2our program so that it wor1s and paste the final code below #This program will demostrate how to use decision #statesments in Python. #This program determines if a bonus should be awarded #The main function def main(): print 'Welcome to the program' monthlySales getSales() #get sales #!unction call to determine bonus is"onus(monthlySales) #!unction call to getermine day off is#ayoff(monthlySales) #This function gets the monthly sales def getSales(): monthlySales input ('$nter the monthly sales %') monthlySales float (monthlySales) return monthlySales #This function determines if there is a bonus def is"onus(monthlySales): if monthlySales & '(((((: print )*ou earned a %+((( bonus,) #This function determines if employess get a day off def is#ayoff(monthlySales): if monthlySales & '-+(((: print ).ll employess get a day off,,,) #calls main main()

Starting Out with Programming Logic and Design

13

Lab 3.* $rogra''ing C"allenge 1 0uess t"e Secrets


Frite the Pseudocode; @lowchart; and P2thon code for the following programming problem.
/elp 0ideo Double clic1 the file to $iew $ideo

0uess t"e Secrets


Write a program that 3i$$ a ? the u er to enter a per onE age- their 3eight- an# their birth month. *our program 3i$$ +ompare the entere# va$ue to the fo$$o3ing an# print the appropriate re pon e . 7e ure to u e mo#u$e . The Secret Answers age = 05 3eight = 10( The Comparison If the gue e# age i $e than or e%ua$ to 05 If the gue e# 3eight i greater than or e%ua$ to 10( If the gue e# birth month i e%ua$ to Apri$ The Printed Response >ongratu$ation - the age i 05 or $e >ongratu$ation - the 3eight i 10( or more >ongratu$ation - the birth month i Apri$

birth'onth = FApri$E

GintB Thi program +an be 3ritten a fe3 #ifferent 3ay . It i ugge te# that on$y three variab$e are u e# to tore the gue e# an 3er to age- 3eight- an# birth'onth. The p eu#o+o#e for main might $oo? a fo$$o3 B 'o#u$e main 56 ;;8e+$are $o+a$ variab$e 8e+$are Integer age 8e+$are Integer 3eight 8e+$are 4tring birth'onth ;;=un+tion +a$$ >a$$ getAge5age6 >a$$ getWeight53eight6 >a$$ get'onth5birth'onth6 >a$$ +orre+tAn 3er 5age- 3eight- birth'onth6 "n# 'o#u$e If the program i $oo? a fo$$o3 B 3ritten proper$yamp$e output hou$#

4amp$e 1B //Input "nter your gue

for ageB 0)

Starting Out with Programming Logic and Design


"nter your gue "nter your gue for 3eightB 14) for birth monthB 'ar+h

14

//Output >ongratu$ation - the age i 05 or $e . >ongratu$ation - the 3eight i 10( or more. 4amp$e 0B //Input "nter your gue "nter your gue "nter your gue

for ageB H0 for 3eightB 112 for birth monthB Apri$ Apri$.

//Output >ongratu$ation - the birth month i 4amp$e HB //Input "nter your gue "nter your gue "nter your gue

for ageB 5( for 3eightB 10( for birth monthB Ianuary 10( or more.

//Output >ongratu$ation - the 3eight i

!"e $seudocode
'o#u$e main 56 ;;8e+$are $o+a$ variab$e 8e+$are Integer age 8e+$are Integer 3eight 8e+$are 4tring birth'onth ;;=un+tion +a$$ >a$$ getAge5age6 >a$$ getWeight53eight6 >a$$ get'onth5birth'onth6 >a$$ +orre+tAn 3er 5age- 3eight- birth'onth6 "n# 'o#u$e ;;Thi mo#u$e ta?e in the age 'o#u$e getAge5Integer <ef age6 8i p$ay "nter your gue for ageB Input age "n# mo#u$e ;;Thi mo#u$e ta?e in the 3eight 'o#u$e getWeight5Integer <ef 3eight6 8i p$ay "nter you gue for 3eightB Input 3eight "n# mo#u$e

Starting Out with Programming Logic and Design

1G

;;Thi mo#u$e ta?e in the birth month 'o#u$e get'onth54tring <ef birth'onth6 8i p$ay "nter your gue for birth monthB Input birthmonth "n# mo#u$e ;;Thi mo#u$e 3i$$ #etermine if the gue e are +orre+t 'o#u$e +orre+tAn 3er 5Integer age- Integer 3eight- 4tring birthmonth6 If age =< 05 8i p$ay >ongratu$ation - the age i 05 or $e !! "n# if If 3eight = 10( 8i p$ay >ongratu$ation - the 3eight i "n# if 10( or more!!

If birthmonth = Apri$ 8i p$ay >ongratu$ation - the birthmonth i "n# if "n# mo#u$e

Apri$!!

!"e #lo(c"art

Starting Out with Programming Logic and Design

1I

Starting Out with Programming Logic and Design

16

!"e $.t"on Code KSecret "nswers KThe main function def main=? print Kprints a blan1 line age + get"ge=? weight + getFeight=? birthmonth + getBonth =? print correctanswers =age; weight; birthmonth? KThis function will input the age def get"ge=? age + input =L-nter 2our guess for age L? return age KThis function will input the weight def getFeight=? weight + input=L-nter 2our guess for weight L? return weight

Starting Out with Programming Logic and Design KThis function will input the birthmonth def getBonth=? birthmonth + raw!input =L-nter 2our guess for the birth month L? return birthmonth KThis function will determine is the answers are correct def correctanswers =age; weight; birthmonth? if age *+ 4G print LCongratulations; the age is 4G or less.M if weight +145 print LCongratulations; the weight is 145.M if birthmonth + "pril print LCongratulations; the birth month is "pril.9 Kcalls main main=?

15

You might also like