Contents

  1. binaryToDecimal.wfunc.py
  2. menu.py
  3. menu_withfunctions.py
  4. non_function_vars.py
  5. oldmac.py
  6. scope.py

binaryToDecimal.wfunc.py 1/6

[
top][prev][next]
# Convert binary numbers to decimal numbers
# CS111

import sys

def main():
    
    print 
    print "This program converts binary numbers to decimal numbers."
    print
    
    binary_string = raw_input("Enter a number in binary: ")
    
    if not isBinary(binary_string):
        print "That is not a binary string"
        sys.exit()

    print "The decimal value is", binaryToDecimal(binary_string)

# returns True iff the string is binary (contains only 0s or 1s)
def isBinary(string):
    if not string.isdigit() :
        return False

    for bit in string:
        if bit != "0" and bit != "1":
            return False
    
    return True

# Given a binary string, returns the decimal value of that binary string.
def binaryToDecimal(bin_string):
    exponent = len(bin_string)-1
    
    dec_value = 0
    
    # for each bit in the binary string,
    # multiply the bit by 2 to the appropriate power
    # and add that to the decimal value, dec_value
    for bit in bin_string:
        bit = int(bit)
        #print bit,"* 2^%d" % exponent
        dec_value += bit * (2 ** exponent)
    
        exponent -= 1
   
    return dec_value
    
main()

menu.py 2/6

[
top][prev][next]
# Module that contains useful menu functions
# Sara Sprenkle


STOP_OPTION = 'Q'


# Displays a formatted welcome message, for a 
# given program named "name"
def printWelcomeScreen(name):
    welcome = "Welcome to " + name + "!"
    length = len(welcome)
    print length*"-"
    print welcome
    print length*"-"


# Display a user's menu options
def printMenu():
    print "You have some options for what to do: "
    print "Enter an 'F' to find a song"
    print "Enter an 'S' to sort by Song title"
    print "Enter an 'A' to sort by Album"
    print "Enter an 'R' to sort by aRtist name"
    print "Enter an 'H' to list your options again"
    print "Enter a 'Q' to quit"

menu_withfunctions.py 3/6

[
top][prev][next]
# Using functions from menu module
# by Sara Sprenkle

import menu


menu.printWelcomeScreen("Music Manager")
menu.printMenu()

menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.upper()

while menuChoice != menu.STOP_OPTION :
    print "Do something appropriate for", menuChoice
    menu.printMenu()
    menuChoice = raw_input("Which option do you choose? ")
    menuChoice = menuChoice.upper()

non_function_vars.py 4/6

[
top][prev][next]
# Using variables that aren't part of any function
# by Sara Sprenkle

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

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 = 6
    non_func_string = "dog"
   
func()
print non_func
print non_func_string

oldmac.py 5/6

[
top][prev][next]
# Print out verses of the song Old MacDonald
# Sara Sprenkle
#

BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"

def main():
    # call the verse function to print out a verse
    verse("dog", "ruff")
    verse("duck", "quack")
    
    animal_type = "cow"
    animal_sound = "moo"
    
    verse(animal_type, animal_sound)
    

# QUESTION: What if called function as
# verse("ruff", "dog")


def verse(animal, sound):
    print BEGIN_END + EIEIO
    print "And on that farm he had a " + animal + EIEIO
    print "With a " + sound + ", " + sound + " here"
    print "And a " + sound + ", " + sound + " there"
    print "Here a", sound
    print "There a", sound
    print "Everywhere a " + sound + ", " + sound
    print BEGIN_END + EIEIO
    print

#main()

if __name__ == "__main__":
    main()

scope.py 6/6

[
top][prev][next]
# scope.py
# Examples illustrating scope
# Note: NOT good coding style
# by Sara Sprenkle

def main():
    n = 30
    e = 1
    f = 2
    g = 3
    h = 4

    print "\nBefore the call to function1,"
    print "n = ", n
    print "e = ", e

    # QUESTION: How to change function1's call to execute other branch?
    i = function1(0, f, g, h)

    print "\nAfter the call to function1,"
    print "n = ", n
    print "e = ", e


def function1(a, b, c, d):
    # QUESTION: What would happen if the following line was commented
    # out?
    n = 400

    print "\nIn function1, "
    print "n = ", n
    print "a = ", a

    if  a >= 1 :
        a += b+n;
        print "a = ", a, "after being modified"
        return a
    else :
        c += d+m+n
        return c

main()

Generated by GNU enscript 1.6.4.