October 24: While and Module review

Learning Objectives

After today's class, you should be able to:

  • Know the difference between local and global scope
  • Understand the role of namescape and scope
  • Review of while loops

Announcements

  • PA 1 Poker Dice is posted: 2 parts
    • FIRST must pass Readiness Quiz on canvas to receive any credit for PA
    • Part A due 10/25 (30 points)
    • Part B due 11/1 (70 points)
  • Today look at test_dice.py
  • Quiz 4A on Thursday: while loops, modules, scope

Mini Lecture on scope

While Loop Reflection

A common pattern for while loops is to process input until some condition is met. For example, imagine we need to add up numbers until the user enters an empty line. Here are some options for implementing:

total = 0
user_entered = input("Enter a number: ")
while user_entered != "":
    num = int(user_entered)
    total += num
    user_entered = input("Enter a number: ")
Pros: Loop condition reveals the purpose of the loop.

Cons: Code repetition

total = 0
while True:
    user_entered = input("Enter a number: ")
    if user_entered == "":
        break
    total += int(user_entered)
Pros: code repetition is gone

Cons: Loop condition is meaningless. We need to dig through the body of the loop to understand the flow of control.

Warning: breakstatements should be used sparingly and with caution. They can make it more difficult for the reader to follow the control flow in your loop.

total = 0
user_entered = "0"
while user_entered != "":
    num = int(user_entered)
    total += num
    user_entered = input("Enter a number: ")
Cons: It can be awkward to find an initial value that doesn't terminate the loop and doesn't violate the loop logic.

Note: Many other languages include a do-while loop construct specifically to make logic of this sort less awkward.

Another example. Let's assume we only want to include the even numbers in the total:

total = 0
while True:
    user_entered = input("Enter a number: ")
    if user_entered == "":
        break
    num = int(user_entered)
    if num % 2 == 1:  # odd
        continue  # Jump to the top and re-check loop condition

    total += int(user_entered)
Warning: continuestatements should be used sparingly and with caution. They can make it more difficult for the reader to follow the control flow in your loop.

Exercise

What will be printed by the following? Hand execute.

x = 10
while x < 1000:
    print(x)
    x *= 2
    if x < 50:
        continue
    if x % 2 == 0:
        x += 1
    if x == 163:
        break

local, global, built-in namespaces and scope

Recall:

  • scope: The area of code where a name is visible. This is a concept, not a part of the Python language.
  • namespace: A mapping from names to objects. Namespaces are actual entities in a Python program
GRAVITY = 9.81  # Global constants should in all-caps.

def cool_function(some_param):
    some_local = some_param * 10
    print(f"Locals in function:\n {locals()}\n\n")
    print(f"Globals in function:\n {globals()}\n\n")
    return some_local

result = cool_function(2)

print(f"Locals:\n {locals()}\n\n")
print(f"Globals:\n {globals()}\n\n")
print(f"Built-ins:\n{dir(__builtins__)}\n\n")
if 'print' in dir(__builtins__):
    print ("yes")

Work Time for PA

  • Review test_dice.py

Planning

By today

  • 100% on the readiness quiz and finish dice.py. Start test_dice.py if you haven't already. PA 1 Part A is due Wednesday

Prepare for Quiz 4A

  • Thursday, beginning of class, 25 minutes, in EnGeo 2204.
  • Two parts: concepts and programming (will use lab machine account, canvas and gradescope)
  • Study chapters 7 and 8: both reading and activities
  • Review class activities
  • Review homework programming assignments

Due Wednesday October 25th at 11pm

  • First, pass the Readiness Quiz on canvas
  • Submit PA 1 Part 1 through Canvas

  • Due Wednesday November 1st at 11pm

  • Submit PA 1 Part 2 (70 points) through Canvas

  • Remember the Help page