Starting from:

$30

ITI#1120 Lab###3

ITI#1120
Lab###3#
Boolean expressions, if statements, debugging, and …
1
What#is#in#this#lab?#
1. This#lab#bas#3#Tasks#(3rd#task#has#several#parts).#You#should#try#
to#do#these#parts#at#home.##
Task#2#and#3#are#in#the#interacCve#textbook.#Watch#the#
video#I#provided#(link#of#page#7)##before#doing#these#two#
tasks.##
2. and#4#programming#exercises#
The#slides#that#are#included#here#but#do#not#explain#either#Tasks#
or#Programming#exercises#are#there#for#you#to#remind#you#of#
some#relevant#material#that#is#needed#for#this#lab.#
2
#Lab#3#overview#
• Open#a#browser#and#log#into#Blackboard#Learn#
• On#the#leM#hand#side#under#Labs#tab,#find#lab3#material#
contained#in#either##lab3Pstudents.zip#or#lab3Pstudnets.pdf#file#
• Download#that#file#to#the#Desktop#and#open#it.#
• Read#slides#one#by#one#and#follow#the#instrucCons#explaining#
what#to#do.#When#stuck#ask#for#help#from#TA.#Ideally#you#
should#try#to#complete#some,#or#all#of#this,##at#home#and#then#
use#the#lab#as#a#place#where#you#can#get#help#with#things#you#
may#have#had#difficulCes#with.
3
Before#starCng,#always#make#sure#
you#are#running#Python#3#
This#slide#is#applicable#to#all#labs,#exercises,#assignments#…#etc##
ALWAYS#MAKE#SURE#FIRST#that#you#are#running#Python#3.4#(3.5 is#
fine#too)#
That#is,#when#you#click#on#IDLE#(or#start#python#any#other#way)#
look#at#the#first#line#that#the#Python#shell#displays.#It#should#say#
Python#3.4#or#3.5##(and#then#some#extra#digits)#
If#you#do#not#know#how#to#do#this,#read#the#material#provided#
with#Lab#1.#It#explains#it#step#by#step#
4
div#//#and#mod#%##operators#in#
Python#
If#uncertain,#here#is#how#to#compute a//b and a%b
1.##Compute#x=a/b
2. a//b is#then#equal#to#the#whole#(i.e#integer)#part#of#x
3. a%b is#equal#to##a - (a//b) * b
5
6
Task#1#
What#is#the#type#and#value#of#each#of#the#following#expressions#in#Python?#Do#this#
in#you#head#(and/or#paper)#first.#The#check#both#columns#in#Python#shell.#eg.#You#
can#test#what#kind#of#value#the#first#expression#returns#by#typing#
type(13*0.1) in#Python#shell
Expression Type Value
13 * 0.1 float 1.3
int(13) * 0.1
13 * int(0.1)
int(13 * 0.1)
13 % 7
6%3
6//2.5
6%2.5
2<3== 4<5
InteracCve#textbook#
Follow#this#link#and#watch#(at#home)#an#instrucConal#video##I#made:#
It#gives#a#brief#introducCon#on#how#to#use#the#interacCve#textbook#
https://www.dropbox.com/s/b3njtf9q8tyc83f/lab3-video-material.mp4?dl=0
Here#is#a#link#to#the#interacCve#textbook:#
http://interactivepython.org/runestone/static/thinkcspy/toc.html
7
Task#2:#Debugging#
Follow#this#link,#and#read,#run#and#do#the#exercises#there:#
http://interactivepython.org/runestone/static/thinkcspy/Debugging/HowtoAvoidDebugging.html
8
An#example#used#in#the#above#link#refers#to#the#following#problem#from#
Chapter:#Simple#Python#Data,#SecCon:#Exercises,#QuesCon#3:#
Many people keep time using a 24 hour clock (11 is 11am and 23
is 11pm, 0 is midnight). If it is currently 13 and you set your
alarm to go off in 50 hours, it will be 15 (3pm). Write a Python
program to solve the general version of the above problem. Ask
the user for the time now (in hours), and then ask for the
number of hours to wait for the alarm. Your program should
output what the time will be on the clock when the alarm goes
off.
Boolean#Expressions#
Boolean#expressions#evaluate#to#True or#False
Logical/Boolean#operators#in#Math#and#Python:
Math ####### Python#
AND and
OR or
NOT not
Here#is#how#your#compare#two#variables#a#and#b#in#Math#and#Python#
######Math ####### ######Python
a = b a == b
a ≤ b a <= b
a ≥ b a >= b
a ≠ b a != b
Truth#table#
A#TRUTH#TABLE for#a#compound#Boolean#expression#shows##
the#results#for#all#possible#combinaCons#of#the#simple#expressions:
TesCng#if#two#strings#are#equal ##
For#one#of#the##programming#exercise#you#will#need#to#know#that#how#to#
compare#if#two#strings#are#equal.#You#can#do#that#simply#buy#using#==
operator.#Here#are#some#examples#
11
>>> ‘A’==‘A’
True
>>> ‘Anna’==‘Anna’
True
>>> ‘Anna’=‘anna’
False
>>> a=‘June’
>>> a==‘june’
False
>>> a==‘June’
True
>>> b=‘Ju’ + ’ne’
>>> a==b
True
ps.#Do#not#copy#paste#the#above#into#python#shell.#It#will#likely#give#you#syntax#
errors#since#quotes#do#not#copy/paste#correctly#from#Word.#
12
Examples#of#compound#boolean#expressions:
• This#is#how#you#would#test#if#age is#at#least#18#and#at#most#65:#
######### age>=18 and age <= 65
• not#is#an#operator#to#negate#the#value#of#a#simple#or#compound#Boolean#
expression.#Suppose#age = 15.#Then:#
##########age > 16 evaluates#to#False,#and#not(age > 16) evaluates#to#True
• Suppose#day#refers#to#a#string#which#is#a#day#of#a#week.#Here#is#how#you#
would#test#if##day#is#a#weekend:#
###################day==“Saturday” or day==“Sunday”
• Here#are#two#ways#to#test#if#age#is#less#than#18#and#greater#than#65.#Think#
about#the#2nd#one#
– 1st#way: age<18 and age > 65
– 2nd#way:# not(age>=18 and age <= 65)
Task#3:#if#statements#
Follow#all#the#links#below.#Read,#run#and#do#the#exercises#in#each#of#them.#No#need#to#
watch#the#videos.#
Boolean#Expressions#
http://interactivepython.org/runestone/static/thinkcspy/Selection/BooleanValuesandBooleanExpressions.html
Logical/Boolean#operators#
http://interactivepython.org/runestone/static/thinkcspy/Selection/Logicaloperators.html
PrecedenceOfOperaCons
http://interactivepython.org/runestone/static/thinkcspy/Selection/PrecedenceofOperators.html
TwoPway#if#statement:#
http://interactivepython.org/runestone/static/thinkcspy/Selection/ConditionalExecutionBinarySelection.html
OnePway#if#statement:#
http://interactivepython.org/runestone/static/thinkcspy/Selection/OmittingtheelseClauseUnarySelection.html
Chained#condiConals#(i.e elif)#
http://interactivepython.org/runestone/static/thinkcspy/Selection/Chainedconditionals.html
Nested#if#statemetns
http://interactivepython.org/runestone/static/thinkcspy/Selection/Nestedconditionals.html 13
Programming#exercises:#QuesCon#1 ##
Write#func&on#called#pay that#takes#as#input#an#hourly#wage#and#the#
number#of#hours#an#employee#worked#in#the#last#week.#The#funcCon#should#
compute#and#return#the#employee’s#pay.#OverCme#work#should#be#paid#in#
this#way:#Any#hours#beyond#40#but#less#than#or#equal#60#should#be#paid#at#1.5#
Cmes#the#regular#hourly#wage.#Any#hours#beyond#60#should#be#paid#at#2#
Cmes#the#regular#hourly#wage.#Important:##
Important:#Note#that#I#did#not#give#you#either#the#number#of#parameters#or#
the#names#of#parameters.#You#will#have#to#figure#it#out#on#your#own#for#this#
funcCon.#Looking#at#the#test#examples#below#should#help.#
14
Example#tests:#
>>> pay(10, 35)
350
>>> pay(10, 45)
475.0
>>> pay(10, 61)
720.0
Programming#exercises:#QuesCon#2 ##
Rock,#Paper,#Scissors#is#a#twoPplayer#game#in#which#each#player#chooses#one#
of#three#items.#If#both#players#choose#the#same#item,#the#game#is#Ced.#
Otherwise,#the#rules#that#determine#the#winner#are:##
(a) Rock#always#beats#Scissors#(Rock#crushes#Scissors)##
(b) #Scissors#always#beats#Paper#(Scissors#cut#Paper)#
(c) #Paper#always#beats#Rock#(Paper#covers#Rock)#
Write#a!!func&on!called#rps#that#takes#the#choice##'R',#'P',#or#‘S'#of#
player#1#and#the#choice#of#player#2,#and#returns#−1#if#player#1#wins,#1#if#player#
2#wins,#or#0#if#there#is#a#Ce.”#
Note#that#I#did#not#give#you#either#the#number#of#parameters#or#the#names#of#
parameters.#You#will#have#to#figure#it#out#on#your#own.#Looking#at#the#
example#runs##below#should#help#too:#
Example#tests:
>>> rps('R', 'P')
1
>>> rps('R', 'S')
-1
>>> rps('S', 'S')
0
15
16
Programming#exercises:#QuesCon#3a
Open#a#new#file#with#IDLE.#Write#a!program!that#has#a#funcCon#called#
is_divisible
• The#funcCon is_divisible has#two#input#parameters#that#are#integers#
n#and#m#and#returns#True#if#n#is#divisible#by#m and#False#otherwise.#
• Outside#of#that#funcCon,#your#program#should#interact#with#the#user#to#get#
two#integers.#To#determine#if#the#1st#is#divisible#by#the#2nd#it#should#call#
is_divisible##funcCon.#It#should#print#a#message#explaining#the#result.##
Two#example#tests#(one#on#the#leM#and#one#on#the#right)#
>>>
Enter 1st integer:
9
Enter 2nd integer:
3
9 is divisble by 3
>>>
Enter 1st integer:
8
Enter 2nd integer:
3
8 is not divisble by 3
17
Programming#exercises:#QuesCon#3b
Open#a#new#file#with#IDLE.#Write#a!program!that#has#two#funcCons#one#
called#is_divisible#and#the#other#called#is_divisible23n8
• The#funcCon is_divisible is#the#same#as#in#the#previous#quesCons#
so#you#can#copy/paste#it#to#the#beginning#of#the#new#file.#
• The#funcCon#is_divisible23n8 has#one#input#parameter,#an#
integer.#It#should#return#string#“yes” if#the#given#number#is#divisible#by#
2#or#3#but#not#6.#Otherwise#it#should#return#a#string#“no”.#Your#funcCon#
is_divisible23n8 must#use,#i.e#make#a#call#to,##is_divisible
• Outside#of#that#funcCon,#your#program#should#interact#with#the#user#to#
get#one#integer.#It#should#call#is_divisible23n8##funcCon#to#
deterimen#if#the#number#the#user#gave#is#divisible#by#2#or#3#but#not#6.#It#
should#print#a#message#explaining#the#result.#
>>>
Enter an integer: 18
18 is divisible by 2 or 3 but not 8
>>>
Enter an integer: 16
It is not true that 16 is divisible by 2 or 3 but not 8
>>>
Enter an integer: 3
3 is divisible by 2 or 3 but not 8
Bonus#programming#exercises:###
For#those#who#are#done#and#want#to#more#programming#
exercises#for#the#lab#or#home,#follow#this#link#and#complete#
any,#or#ideally#all,#exercises#there:#
http://interactivepython.org/runestone/static/thinkcspy/
Selection/Exercises.html
18