Authors: Mohamed Gaber, Rebecca Nesson, Trisha Stan
Views : 172
Description: ATT Code Lab Session 2.2

# ATT Code Lab

## Session 2

Welcome to the second Code Lab for Applied Algorithmic Thinking! In this Lab, you will work with a colleague or two to work through a series of scaffolded problems to build knowledge and gain confidence in your programming skills. Take the time to work through each of the problems in order with your partner(s). Be sure that everyone understands each part of the code. Take turns explaining the concepts to each other; this is a great way to check your understanding. Work through as many problems as you can in this first session. After 20 minutes, you will come back to the whole class for a brief discussion of key concepts. Then you will go into new groups for a second 15 minute breakout session to continue the work. When the second session begins, introduce yourself to your partner(s), then take a moment to ask (and try to answer) any remaining questions, then continue working through the notebook. Then, then discuss how far each of you got through the workbook in the first breakout and pick up where you left off to continue through the workbook. Happy coding!

## Welcome

During every lab session you will be working in a small group with your peers. We have designed the class this way because when you learn actively and collaboratively you learn more and have more fun doing it. As you work together, it will be easier if you first take a few minutes to get to "break the ice" with each other. So in each lab session we will begin with an Ice Breaker activity.

### Ice Breaker

2. If you could have a superpower, what would it be and what would you do with it?

Share your superpowers with the teaching staff!

If your friend (of the buggy code above) were going to do problems like A.4 and A.5, what one piece of advice would you give her?

# Section B: Control Flow and Conditional Branching

How does Python know which line of code to execute next? Well, isn't it obvious that it should start at the first line, and then go to the second, and so on? While that's the general idea, we've already seen that Python does something different with loops. With the loops we've seen so far, it runs a group of lines repeatedly before progressing to the next line. Loops are one way of changing the flow of control from just going from top to bottom. In this section we will learn about how to make control flow branch based on the answer to a question.

#### B.1

In the last class we learned a bit about the == operator and other comparison operators. These operators compare two values and produce a value of True or False depending on whether the comparison is valid. The following comparisons are all True. Select a different value for x to make them all False.

In :
x = 6
print(4+2 == x)
print(x * x > 30)
print(x / 2 != 2)
print(x >= 5)


True True True True

#### B.2

We can think of these True and False values (known as Boolean values after logician George Boole) as the answers to our questions. When we write 4+2 == x what it means is "4+2 equals the value stored in the variable x?" The result is our answer: True if x is 6 and False otherwise. We can make use of these answers to have our programs do something different depending on the answers. For instance, we might want to do one thing if 4+2 does equal x and another thing if it does not. To do this in python we use if statements.

Consider the following code. What does it do? Think about what input values you should try to explore the possible behaviors.

In :
age = int(input("What is your age? "))

print("Bus tickets cost $2.50.") if age < 12: print("You qualify for a discounted bus ticket for only$0.50!")

Bus tickets cost $2.50. You qualify for a discounted bus ticket for only$0.50!

#### B.3

Suppose that the rules for bus tickets say that you get a discounted bus ticket if you are 12 or under. Is the above code a correct implementation? If not, how can you fix it?

In [ ]:
age = int(input("What is your age? "))

print("Bus tickets cost $2.50.") if age < 12: print("You qualify for a discounted bus ticket for only$0.50!")


#### B.4

You've done such a great job fixing the bus ticket app that the city wants more of your expertise. They also have a discount for teens (age 18 and under) but the coder who worked on it was unable to implement it. Here is the code that she produced. Can you explain what's going wrong and why? What input values demonstrate the problem?

In :
age = int(input("What is your age? "))

print("Bus tickets cost $2.50.") if age <= 12: print("You qualify for a discounted bus ticket for only$0.50!")
if age <= 18:
print("You qualify for a teen discount bus ticket for only $1.00.")  What is your age? Bus tickets cost$2.50. You qualify for a discounted bus ticket for only $0.50! You qualify for a teen discount bus ticket for only$1.00.

#### B.5

One way to fix the problem above is with more specific constraints in the Boolean expression to the if. Three other useful Boolean operators--and, or, and not--take Boolean expressions as inputs and combine them to produce a Boolean result. When you use the boolean operators and, or, and not you should be sure that the expression that you give to them are Boolean type expressions. (Note: Though Python will let you use Boolean operators on other types of values, it is bad style to do so as the code will be hard to understand.)

Guess what each of the following expressions will evaluate to before you run the code.

In :
print(True and True)
print(True and False)
print(False and False)

print(True or True)
print(True or False)
print(False or False)

print(not True)
print(not False)

x = 6
y = 4

print(x > 5 and y > 5)
print(x > 5 or y > 5)
print(x > 5 and not y > 5)


True False False True True False False True False True True

#### B.6

Can you use these new operators to fix the bus ticket code so it only prints one, accurate discount message for people 12 and under? Hint: Try modifying the boolean expression of the second if.

In [ ]:
age = int(input("What is your age? "))

print("Bus tickets cost $2.50.") if age <= 12: print("You qualify for a discounted bus ticket for only$0.50!")
if age <= 18:
print("You qualify for a teen discount bus ticket for only $1.00.")  #### B.7 In the last problem you could fix it by modifying your conditions, but sometimes you want your code to take one action if a condition is true and another action if it is false. To handle this situation, Python (and most other programming languages) provide a way to specify actions when the condition is false, called an else clause. Here's a simple example: x = int(input("enter a number")) if x >= 0: print("it is positive") else: print("it is negative")  It turns out that people are getting confused about whether their bus tickets cost$2.50 or $0.50 because it prints both messages. The city decides to make all bus tickets either full price ($2.50) or discounted ($0.50). So now all people 18 or under and all people 65 or over get discounted tickets and everyone else gets full price tickets. They ask you to rewrite the program so that it just prints out one line of text giving the ticket price. If the user is, say, 15 or 75, the program should print "Bus tickets cost$0.50". If the user is say, 35, the program should print "Bus tickets costs \$2.50". All these changes are a great way to practice your coding, so you take on the challenge. Can you write the code using Python's if/else?

In [ ]:



# Section C: Problem Solving

### Leap Years

Ask the user to input a year. Print "It is a leap year!" if it is a leap year and print "It is just a plain old year" if it is not. Leap years are those years that are multiples of 4 except that if they are also multiples of 100 they are only leap years if they are not multiples of 400. So, for instance, the year 2000 was a leap year but the year 2100 is not a leap year.

### Truth Telling Step 1

Consider the following situation:

1. Malfoy says: “Harry is a liar!”
2. Harry says: “I’m the opposite of Malfoy”


Who is telling the truth and who is lying?

Let the variable m represent the statement "Malfoy is a truth teller" and let the variable h represent the statement "Harry is a truth-teller". Then if m = True, it means that Malfoy always tells the truth. And if m = False, it means Malfoy always lies.

To answer our question we can translate both statements into boolean expressions that will evaluate to true only if the statements are true. For example, we can translate the first statement as:

s1 = (m and not h) or (not m and h)

This means that in order for Malfoy's statement to be true, it must be the case that Malfoy is truth teller and that Harry is a liar.

Write a Boolean expression op that means Harry is the opposite of Malfoy. That is, op should be true only if h is True and m is False or h is False and m is True.

In [ ]:
m = True     # These are placeholder truth values. You can change them to test out your expressions.
h = True

s1 = (m and not h) or (not m and h)

op =         # FILL IN THIS LINE


### Truth Telling Step 2

Now write a Boolean expression s2 that represents Harry's statement. That is, it should be true if Harry is a truth teller and Harry and Malfoy are opposites and it should be true if Harry is a liar and Harry and Malfoy are not opposites. Use your expression op in s2 to make writing s2 easier.

In [ ]:
s2 =         # FILL IN THIS LINE


### The Truth Revealed

You can now figure out who is telling the truth and who is lying! How? Let's find a truth assignment that makes both s1 and s2 true at the same time. Try assigning all combinations of truth values to m and h to see which one makes the following expression True.

How can you be sure you are right? Only one truth assignment to m and h should work. Also, if you're a Harry Potter fan you should already know the answer!

In [ ]:
s1 and s2


answer = input("Welcome to the guru. Do you like cheese? Enter 1 for yes and 0 for no")   # Here's a starting point. Please change it to fit your advice topic.