Difference between revisions of "CSC111 Python Programs Fall 2015"

From dftwiki3
Jump to: navigation, search
(Monday, 9/21/15)
(Monday, 9/21/15)
Line 122: Line 122:
 
</source>
 
</source>
 
<br />
 
<br />
 +
=Wednesday, 9/23/15=
 +
<br />
 +
<source lang="python">
 +
# 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()
 +
</source>
 +
<br />
 +
* Accumulating Strings
 +
<source lang="python">
 +
# 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()
 +
 +
</source>
 +
<br />
 +
=Friday, 9/25/15=
 +
<br />
 +
<source lang="python">
 +
# 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()
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Python]]
 
[[Category:CSC111]][[Category:Python]]

Revision as of 08:33, 25 September 2015

--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()