Lab 3 Student
Lab 3 Student
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
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
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
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.
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
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
>
1C
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
11
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()
13
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$#
for ageB 0)
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.
!"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
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!!
Apri$!!
!"e #lo(c"art
1I
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