CSC111 Python Programs Fall 2015

From dftwiki3
Revision as of 14:20, 2 October 2015 by Thiebaut (talk | contribs) (Wed., 9/30/15)
Jump to: navigation, search

--D. Thiebaut (talk) 10:34, 18 September 2015 (EDT)


Wed., 9/16/15


# displayWeek.py
# D. Thiebaut
# displays a schedule for the five days of the week

length = eval( input( "Length of bar? " ) )

print( "-" * length )
for day in [ "Mo:", "Tu:", "We:", "Th:", "Fr:" ]:
    print( day )
    #print( "  :" * 2)
    print( "  :" )
    print( "-" * length )


Friday, 9/18/15


# displayGrade.py
# D. Thiebaut
#
# prompts user for information and
# grade.
# display user's grade in graph, along with
# class average (constant)


# constant information
classAvg = 80

# user input.  Get info from user
fName = "Al"
lName = "K"
Id    = "990123456"
final = 90


#--- Format information and display ---
# create the bar 
bar = "+" + 48*"-" + "+"
lenBar = len( bar )

# compute the number of dashes
noSpaces = lenBar - len( fName ) -len(lName )-len(Id) - 6
spaces = " " * noSpaces

# create the scale
scale = "      00...10...20...30...40...50...60...70...80...90...100"

# display the information
print( bar )
print( "|" + fName, lName, spaces, Id, "|" )
print( bar )
print()
print( scale )
# the length of the bar for the bar-graph, in number of characters
# is 1/2 the actual grade.  So divide the grade by half to get the
# number of chars.  We use // to get the integer part of the result
print( "grade:", (final//2) * "#" )
print( "class:", (classAvg//2) * "#" )


Monday, 9/21/15


Version 1 of Teller Machine program

# tellerMachine.py
# D. Thiebaut
# A program that simulates a teller machine, where user enters an amount of
# dollars, and program figures out the number of bills to return.

# get the amount 
amount = eval( input( "Please enter amount: " ) )
amount = int( amount )
print()

# break down into bills
no20s = amount // 20       # how many 20s go into amount
amount = amount % 20    # what is left over after giving out 20s go back into amount

no10s = amount // 10
amount = amount % 10

no5s = amount //5
no1s = amount % 5

# display the results
print( "Amount withdrawn = ", amount )
print( "Please lift your keyboard and find: " )
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )


Version 2, submitted by Shirui Cheng, who figured out that it was a perfect opportunity to use a loop!

# Teller machine simulator
# Shirui Cheng

# get the input
amount = eval(input ("How much money do you want to withdraw? "))

# break down in $20-, $10-, $5-, and $1-bills
# and print the result at every step
for bill in (20, 10, 5, 1):
    no = amount // bill
    amount = amount % bill
    print (no, "$", bill, "bill(s)")


Wednesday, 9/23/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about computing
# the average value of a list of numbers.
#
def main():

    # initialize variables
    sum = 0
    count = 0

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "average = ", sum/count )

main()


Accumulating Strings


# stringPatterns.py
# D. Thiebaut
# print a string of 5 alternating patterns.

def main():
    # define 2 different patterns
    pat1 = "**"
    pat2 = "++"

    # create an empty string
    result = ""

    # loop 5 times (we want a string of 5 alternating patterns)
    for i in range( 5 ):
        # add a new pattern to the string
        result = result + pat1

        # switch the patterns around
        pat1, pat2 = pat2, pat1

    # done with the loop!  Print string of patterns
    print( result )

main()


Friday, 9/25/15


# averageAge.py
# D. Thiebaut
#
# Example of how one would go about debugging a simple
# program and reveal how the loop works.
#
def main():

    # initialize variables
    sum = 0
    count = 0
    print( "sum = {0:1} count = {1:1}".format( sum, count ) )
    input()

    # loop through a list of ages and compute the total sum
    # of the ages, and the number of values in the list.
    for age in [20, 19, 21, 20, 21, 29, 17]:
        sum = sum + age
        count = count + 1
        print( "age = {0:5} sum = {1:5} count = {2:5}".format( age, sum, count ) )
        input()

    # compute the average, displays quantities of interest
    print( "-" * 30 )
    print( "sum = ", sum )
    print( "count = ", count )
    print( "average = ", sum/count )

main()



Wed., 9/30/15


# exercises 9/30/15

def main():
    # Exercise #1
    fName = "SOPHIA"
    lName = "smith"

    fullName = fName + " " + lName

    print( fullName.title().center( 60 ) )
    print( "\n", "-"*60, "\n\n", sep="" )
   
    # Exercise #2
    # Define the string with multiple lines
    book = """Ulysses
    James Joyce
    Stately, plump Buck Mulligan came from the stairhead, 
    bearing a bowl of lather on which
    a mirror and a razor lay crossed. 
    """

    # split the book into a list of lines
    lines = book.splitlines()

    # assign different lines to variables
    bookTitle = lines[0]
    author = lines[1]
    sentence = lines[2] + lines[3] + lines[4]
    # this next statement is too sophisticated for now
    #sentence = " ".join( lines[2: ] )

    # display the result
    print( bookTitle.upper().center( 60 ) )
    print( author.title().center( 60 ) )
    print( )
    print( sentence )

main()


Fri., 10/2/15


# Using split()
# -------------------------
poem = """Chocolate
Chocolate is the first luxury.
It has so many things wrapped up in it:
Deliciousness in the moment,
childhood memories,
and that grin-inducing
feeling of getting a reward for being good.
--Mariska Hargitay"""

# display each line centered in 60 spaces.
# first line all uppercase.
# last line right justified in 60 spaces.

lines = poem.split( "\n" )
#print( lines )

# (remove the triple double-quotes to energize the code section)
"""
print( lines[0].upper().center(60) )

for line in lines[1: ]:
    print( line.center( 60 ) )


lines[0] = lines[0].upper()
for line in lines[0:3]+lines[4:]:
    print( line.center(60) )
"""

# -------------------------
# Another Example
# -------------------------

"""

def printBar():
    print( 60 * '-' )


def sayHello():
    print( )
    print( "Hello, and welcome!" )
    print( )


def main():
    for i in range( 10 ):
        printBar() 
    sayHello()
    printBar()

main()
"""
# ------------------------------------------
# Sing Happy Birthday to some Minions
# ------------------------------------------

def singHappyBirthday( minion ):
    print( "Happy birthday to you\n" * 2, end="" )
    print( "Happy birthday, dear", minion )
    print( "Happy birthday to you" )
    print()
    
def main():
    #singHappyBirthday( "Dave" )
    #singHappyBirthday( "Stuart" )
    minions = "Dave, Stuart, Jerry, Jorge, Tim, Mark, Phil, Kevin,  Jon"

    minions = minions.split( ", " )
    #print( minions )
    
    for minion in minions:
        singHappyBirthday( minion.strip() )
        
main()