Difference between revisions of "CSC111 Programming Examples Week 5"
(Created page with "--~~~~ ---- =Programs developed in class in Week #5= <br /> <source lang="python"> # sing.py # D. Thiebaut # Example of top-down design, and use of functions. # CSC111 week #5...") |
|||
Line 3: | Line 3: | ||
=Programs developed in class in Week #5= | =Programs developed in class in Week #5= | ||
<br /> | <br /> | ||
+ | ==exo1.py== | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | # exercise 1 | ||
+ | # | ||
+ | |||
+ | def main0(): | ||
+ | # input data | ||
+ | fname = "MaX" #input( "first name? " ) | ||
+ | lname = "HEADROOM" #input( "last name? " ) | ||
+ | |||
+ | # reformat data | ||
+ | fullName = fname.capitalize() + " " + lname.capitalize() | ||
+ | fullName = fullName.center( 60 ) | ||
+ | |||
+ | # output data | ||
+ | #print( fname, lname ) | ||
+ | print( fullName+"!" ) | ||
+ | |||
+ | def main(): | ||
+ | # get the input line | ||
+ | line = "01234564135551212adflkja;sdlkfja ;sdflkjads; lfkjasldkfj" | ||
+ | |||
+ | # extract phone number | ||
+ | phone = line[ 7:7+10 ] | ||
+ | |||
+ | # reformat phone number | ||
+ | formattedPhone = "({0:1}) {1:1}-{2:1}".format( phone[0:3], | ||
+ | phone[3:6], | ||
+ | phone[6: ] ) | ||
+ | # output phone number | ||
+ | print( phone ) | ||
+ | print( formattedPhone ) | ||
+ | |||
+ | |||
+ | main() | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==sing.py== | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
# sing.py | # sing.py |
Revision as of 13:01, 26 February 2015
--D. Thiebaut (talk) 08:32, 26 February 2015 (EST)
Programs developed in class in Week #5
exo1.py
# exercise 1
#
def main0():
# input data
fname = "MaX" #input( "first name? " )
lname = "HEADROOM" #input( "last name? " )
# reformat data
fullName = fname.capitalize() + " " + lname.capitalize()
fullName = fullName.center( 60 )
# output data
#print( fname, lname )
print( fullName+"!" )
def main():
# get the input line
line = "01234564135551212adflkja;sdlkfja ;sdflkjads; lfkjasldkfj"
# extract phone number
phone = line[ 7:7+10 ]
# reformat phone number
formattedPhone = "({0:1}) {1:1}-{2:1}".format( phone[0:3],
phone[3:6],
phone[6: ] )
# output phone number
print( phone )
print( formattedPhone )
main()
sing.py
# sing.py
# D. Thiebaut
# Example of top-down design, and use of functions.
# CSC111 week #5
# singHappyDave: sings happy birthday to just Dave
# (This function is very boring, as it always does the same
# thing)
def singHappyDave():
print( "Happy birthday to you," )
print( "Happy birthday to you," )
print( "Happy birthday, dear Dave" )
print( "Happy birthday to you," )
# singHappyDave: sings happy birthday to just Sutart
# (This function is also very boring, as it always does the same
# thing)
def singHappyStuart():
print( "Happy birthday to you," )
print( "Happy birthday to you," )
print( "Happy birthday, dear Stuart" )
print( "Happy birthday to you," )
# printBar: prints a bar made of the character 'char',
# and with 'length' characters
def printBar( char, length ):
print( char * length )
# printBarName: prints a bar made of the name of a person,
# with a variable length defined by 'length'.
# For example, printBar( "Dave", 10 ) will print the string
# "Dave*Dave*" (without the quotes). This string has length
# 10. printBar( "Dave",12 ) would print "Dave*Dave*Da".
def printBarName( name, length ):
line = (name +"*" ) * length
print( line[0:length] )
# singHappy: sings happy birthday to a person whose name
# is passed in the parameter 'name'. Prints a bar above
# and below the song. The bar is the same length as the
# line where the person's name is mentioned.
def singHappy( name ):
# create a string for the part of the line that contains
# the person's name.
line = "Happy birthday, dear"
# print a bar that is the same length as the line
# containing the person's name.
#printBar( "=", len( line ) + len( name ) + 1 )
printBarName( name, len( line ) + len( name ) + 1)
# sing happy birthday
print( "Happy birthday to you," )
print( "Happy birthday to you," )
print( line, name ) # prints "Happy birthday, dear xxxx"
# where xxxx is whatever is in name.
print( "Happy birthday to you," )
# print another bar
#printBar( "=", len( line ) + len( name ) + 1 )
printBarName( name, len( line ) + len( name ) + 1)
# a couple blank lines...
print( "\n\n" )
# createTextFile. Utility function that I will use to
# quickly create a text file with some information in it.
# This function automatically stores the file in the same
# directory where this program is saved.
def createTextFile():
file = open( "names.txt", "w" )
file.write( "Gru Stuart Dave Carl Tim Jorge\n" )
file.close()
# debugging function. Just to test printBarName() on its
# own, without running the rest of the program.
def main0():
printBarName( "Dave", 10 )
# MAIN FUNCTION
# This is where the program starts.
# The code sections between """ quotes are strings,
# and regarded by the interpreter as nothing more than
# a string. We use this to comment out large sections of
# code.
def main():
# create the text file with
# minion names in it. Comment this line once you have
# run your program once (you don't want to keep on
# recreating this file all the time!)
createTextFile()
"""
# sing happy birthdy to Dave and Stuart
singHappyDave()
print()
singHappyStuart()
print()
"""
"""
# sing happy birthday to 2 people, but this
# time we use the same function to perform the
# same task, but pass the name of the person
# via a parameter.
singHappy( "Dave" )
print()
singHappy( "Stuart" )
print()
"""
# Sings happy birthday to a list of people whose
# names are in a string, separated by spaces.
"""
# list of people to sing to...
minions = "Gru Stuart Dave Carl Tim Jorge"
names = minions.split( ' ' )
#print( names )
for name in names:
singHappy( name )
"""
# Read names from a file called names.txt on disk,
# and sing happy birthday to all the people listed
# We assume that all the names are listed on one
# line, all separated by spaces.
file = open( "names.txt", "r" )
line = file.read()
file.close()
# remove whitespace at beginning and end of string
#print( line, "!" ) # debugging statement to check
# if there is a \n at the end
# of line.
line = line.strip() # we found out there was one,
# so we strip the line of
# leading and trailing whitespace
# (space, tab, and \n)
names = line.split( ' ' )
for name in names:
singHappy( name )
# ----------------------------------------------------------
# M A I N
# ----------------------------------------------------------
main()