Css225
Module 8: Lab Activity – Booleans
Deliverables:
· Python program solutions to the following 7 problems
Make Sure You:
· Add comments
# Your name
# The date
# What the program does
· Test your program
· Fix any bugs (try out the debugging techniques you read about)
Game Example from How to Think Like a Computer Scientist:
For example, suppose we can slay the dragon only if our magic lightsabre sword is charged to 90% or higher, and we have 100 or more energy units in our protective shield. We find this fragment of Python code in the game:
|
|
if not ((sword_charge >= 0.90) and (shield_energy >= 100)): print("Your attack has no effect, the dragon fries you to a crisp!") else: print("The dragon crumples in a heap. You rescue the gorgeous princess!") |
de Morgan’s laws together with the logical opposites would let us rework the condition in a (perhaps) easier to understand way like this:
|
|
if (sword_charge < 0.90) or (shield_energy < 100): print("Your attack has no effect, the dragon fries you to a crisp!") else: print("The dragon crumples in a heap. You rescue the gorgeous princess!") |
We could also get rid of the not by swapping around the then and else parts of the conditional. So here is a third version, also equivalent:
|
|
if (sword_charge >= 0.90) and (shield_energy >= 100): print("The dragon crumples in a heap. You rescue the gorgeous princess!") else: print("Your attack has no effect, the dragon fries you to a crisp!") |
Problem 1 – Write a function that takes two inputs from a user and prints whether they are equal or not.
Problem 2 – Write a function that takes two inputs from a user and prints whether the sum is greater than 10, less than 10, or equal to 10.
Problem 3 – Write a function that takes a list and prints if the value 5 is in that list.
Problem 4 – Write a function that takes a year as a parameter and returns True if the year is a leap year, False if it is otherwise.
Consider the requirements of a leap year:
· The year is evenly divisible by 4
· If the year can be evenly divided by 100 it is NOT a leap year, unless:
· If the year is also evenly divisible by 400, then it is a leap year.
Game Character has the following item list: [pan, paper, idea, rope, groceries]
Game Character has the following status debuffs: [slow]
Task 1: Climb a mountain – needs rope, coat, and first aid kit, cannot have slow
Task 2: Cook a meal – needs pan, groceries, cannot have small
Task 3: Write a book – needs pen, paper, idea, cannot have confusion