Starting from:

$25

ITI1120-Lab 3 Boolean Expressions, If Statements and Debugging Solved

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
 
 
6

Interactive    textbook    (Reminder)

Here    is   a    link      to  the interactive      textbook: https://runestone.academy/runestone/static/thinkcspy/index.html

Task 2: Debugging
Follow  this      link,     and     read,  run and     do the exercises  there:

https://runestone.academy/runestone/static/thinkcspy/Debugging/HowtoAvoidDebugging.html

An       example       used      in    the    above    link refers    to    the    following      problem       from    Chapter:       Simple   Python  Data,    Section: Exercises,     Question      3:

https://runestone.academy/runestone/static/thinkcspy/SimplePythonData/Exercises.html

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.

Programming  exercise 0: 

More   debugging  and coding
Open   file     q0.py and    solve  the     3   programming  exercises     as instructed.

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    combinations of  the simple expressions:

 

Testing  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

‘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.     

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 Expressionshttps://runestone.academy/runestone/static/thinkcspy/Selection/BooleanValuesandBooleanExpressions.html

Logical/Boolean operatorshttps://runestone.academy/runestone/static/thinkcspy/Selection/Logicaloperators.html

PrecedenceOfOperationshttps://runestone.academy/runestone/static/thinkcspy/Selection/PrecedenceofOperators.html

Twohttps://runestone.academy/runestone/static/thinkcspy/Selection/ConditionalExecutionBinarySelection.html-way      if    statement:

Onehttps://runestone.academy/runestone/static/thinkcspy/Selection/OmittingtheelseClauseUnarySelection.html-way      if    statement:

Nested https://runestone.academy/runestone/static/thinkcspy/Selection/Nestedconditionals.htmlif statement:

Chained     conditionals     (i.e elif)

https://runestone.academy/runestone/static/thinkcspy/Selection/Chainedconditionals.html

DOCSTRINGS
For all the python functions that you will write in the following questions (and in the future) make sure you document your functions by writing docstrings including the type contract (as we have done in class). 

Test if you docstrings appear as help of your function by running help(your_function_name) in python shell.

Programming   exercises:   Question 1
Write   function called   pay that      takes     as    input     an   hourly      wage     and the number of    hours      an   employee    worked in    the      last week.    The function      should   compute      and return      the employee’s  pay.       Overtime      work      should   be   paid       in      this way:      Any hours     beyond      40   but less than       or    equal      60   should   be   paid       at    1.5      times     the regular  hourly   wage.      Any hours     beyond 60   should      be   paid       at    2     times     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 function.      Looking at      the test examples     below      should   help.

Example  tests:

pay(10, 35) 

350 

pay(10, 45) 

475.0

pay(10, 61) 

720.0

Programming   exercises:   Question 2
Rock,     Paper,   Scissors is     a two-player   game     in    which each      player    chooses one of three     items.    If     both      players choose  the same     item,      the game     is     tied.       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             function       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     tie.”

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') 



rps('R', 'S') 

-1

rps('S', 'S') 

0

Programming  exercises:  Question 3a
Open a     new       file  with       IDLE.   Write     a     program       that        has a   function       called    is_divisible

•            The     function 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        function,     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 function.   It     should   print      a     message       explaining    the result.    

Two   example       tests      (one       on   the   left 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

Programming   exercises:   Question 3b
Open     a     new       file  with       IDLE.       Write     a     program       that        has       two functions      one called       is_divisible and   the other       called    is_divisible23n8

•The       function is_divisible is       the same     as    in    the previous       questions     so   you can copy/paste       it     to    the beginning     of    the new       file.

•The       function       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 8.    Otherwise       it     should   return   a     string       “no”.   Your      function       is_divisible23n8 must     use,       i.e   make     a     call  to,          is_divisible

•Outside       of    that        function,       your       program       should   interact       with       the user       to    get  one       integer. It     should   call       is_divisible23n8 function       to    deterimen   if     the number the       user       gave       is     divisible by   2       or    3     but not 8.    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:

More products