Contents

  1. non_function_vars.py
  2. pick4winner_wfunctions.py
  3. practice1.py
  4. practice2.py
  5. practice3.py

non_function_vars.py 1/5

[
top][prev][next]
# Using variables that aren't part of any function.
# Not covered in class, but may be of interest to some students.
# by Sara Sprenkle

# create variables that aren't part of any function
non_func = 2
non_func_string = "aardvark"

def main():
    func()
    print(non_func)
    print(non_func_string)

def func():
    print("In func: nf =", non_func)
    print("In func: nfs =", non_func_string)

    # Question: what happens when we try to assign the variables that
    # aren't part of a function a value?
    # non_func = 7
    # non_func_string = "zebra"
    # Answer: 
    
    
main()
non_func = 6
non_func_string = "dog"
print(non_func)
print(non_func_string)
main()

pick4winner_wfunctions.py 2/5

[
top][prev][next]
# Simulate Pick 4 lottery game - selecting ping pong balls at random
# Shows how use of functions better organizes your program.
# 
# By CSCI111

from random import *
import sys

# define constants that are easy to change so that our
# program is flexible
NUM_PICKS = 4
MIN_VALUE = 0
MAX_VALUE = 9

NUMFORMAT="####"

def main():
    pickedNum = input("What is your pick? (Format: " + NUMFORMAT + ") ")
   
    handleBadChosenNum(pickedNum)
        
    winningNum = findWinningNum()
    
    print("The winning Pick 4 lottery number is ", winningNum)
    print()
    
    if winningNum == pickedNum:
        print("Congratulations!  You are very lucky and rich!")
        print("We should be friends!")
    else:
        print("Sorry, you lost.")
    
def findWinningNum():
    """ 
    Generate the random winning number, based on the constants
    NUM_PICKS, MIN_VALUE, and MAX_VALUE
    """
    winningNum = "" # start it as empty
    
    for i in range(NUM_PICKS):
        # generate a random number
        # add the random number to the previous random number
        winningNum += str(randint(MIN_VALUE,MAX_VALUE))
    
    return winningNum

def handleBadChosenNum(chosenNum):
    """
    Handles if the chosen number is not in the right format, 
    e.g., NUM_PICKS long, containing only numbers
    Prints an error message and exists the program if not a 
    good number.
    """
    
    error = False
    errorMessage = "Error:\n"
    
    # Check that user enters a string that contains only numbers
    if not chosenNum.isdigit():
        errorMessage += "\tYour number must contain only numbers\n"
        error = True
        
    # User enters a number that is not four digits long
    if len(chosenNum) != NUM_PICKS:
        errorMessage += "\tYour number must contain " + str(NUM_PICKS) + " numbers"
        error = True
        
    if error:
        print(errorMessage)
        sys.exit()

main()

practice1.py 3/5

[
top][prev][next]
# Exercising your knowledge of variable scope.
#

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)

def square(n):
    return n * n

main()

practice2.py 4/5

[
top][prev][next]
# Exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)
    print("The original num was", n)

def square(n):
    return n * n

main()

practice3.py 5/5

[
top][prev][next]
# Exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", computed)
    print("The original num was", num)

def square(n):
    computed = n*n
    return computed

main()

Generated by GNU Enscript 1.6.6.