Study Guides (380,000)
CA (150,000)
U of A (4,000)
CMPUT (100)
Midterm

# CMPUT 174 Midterm: cmput174 inclass2.pdf

Department
Computing Science
Course Code
CMPUT174
Professor
Study Guide
Midterm

This preview shows pages 1-2. to view the full 8 pages of the document.
CMPUT IN CLASS AFTER FIRST MIDTERM!
!
FEB 25. 2014!
FUNCTIONS (lecture 8)!
function call made by writing the name of the function!
when you call the function you need to make sure you account for all the perimeters.!
to setup a function:!
def <name>(number it requires)!
then perimeters to complete!
ie.!
#to get user to enter a valid number!
def validnumber():!
notValid=True!
while (notValid== True):!
number=input(‘Enter a number between 0-9: ‘)!
if (number==‘’):!
print(‘empty input, try again’)!
else:!
number=int(number)!
!
try block to use if statement my generate an exception!
try:!
statement that might generate error!
except <type of error>: (if something goes wrong it comes down)!
print message if it comes to error, try again!
else:!
if number is still within valid grounds but still in error it just ends up as false!
!
FEB 27. 2014!
a function making the user enter a string made of alphabets only!
def validString():!
astring=input(‘enter a string made of alphabets only> ‘)!
while (astring.isaplha()==False):!
astring=input(‘Try again! Enter a string made of alphabets only> ‘)!
return a string!
!
def main():!
result=validstring()!
print(result)!
!
main()!
!
every function does a single well-deﬁned task!
return with nothing beside it means return none, the same is with out having return at all!
local variable is deﬁned inside a function, scope is limited inside the function it is deﬁned in*.!
local variable lives and dies with the function it is deﬁned in!
deﬁning main as its own function, keeps local variables alive, when using functions.!
takes in a list and deletes last item.!
!

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

def deletelast(alist):!
if (len(alist)>0):!
alist.pop()!
return!
!
def main():!
mylist=[2,4,6,8]!
*deletelast(mylist)!
print(mylist)!
!
main()!
!
when you reduce the list, a new list is not created, 2,4,6 is printed.!
caller will remember changes that happened to list.!
variables deﬁned just within functions, but to each other as well.!
!
MARCH 6. 2014!
ASSIGNMENT 4: only required to use test cases, not asking user for input, code has to cater
to any dimension of the matrix, variable names for rows and columns. matrix=list inside a list.
LAB 6 ONE EXAMPLE FOR MATRIX MULTIPLICATION. no error checking, info needed is
everything before now.!
TUPLE: like a list is a sequence of objects, but it cannot mutate. But object inside can be
mutable, then you can change that object. Combination of string and list. For when you want
information to be stored in a container in one data structure, when you don't want value to be
changed inside.!
ie. storing name space then grade, then splitting info!
!
info=input(‘Enter name ﬂowed by space followed by name: ‘)!
#info split into two separate variable names!
!
#putting info in tuple data aspect with two structures!
#record[0]= <name>!
!
accessing ﬁle name!
!
MARCH 11. 2014!
opening ﬁle object >> ﬁleVariable=open(ﬁlename, mode)!
in read, append or write mode.!
Good idea to put input of ﬁle name in to try except blocks because they will often generate
errors!
*OPENING NEW FILE: if it doesn't exist it will create a new and write to it, if it does exist, old
contents are overwritten and erased, its safer to check if the ﬁle exists already.!
to check if it exists : import>> import os.path!
using write function >> outﬁle.write (“rejerjhwr /n”)!

Unlock to view full version

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

*You can ONLY write a string to a ﬁle, even if you have a number it has to be converted to
string.!
If you want the stuff to be written in such a manner that every write comes on a new line, you
have to specify the newline character in the message.!
ie 2 print statements automatically puts on two different lines, !
*write statement will not separate into different lines, you have to specify, explicitly so that
every output goes on a new line.!
ie. outﬁle. write (‘words /n’)!
outﬁle.write (‘morewords /n’)!
integer into a string on slides*!
ie how to open a ﬁle!
!
endofprogram= False!
!
try:!
ﬁlename=input('enter name of ﬁle')!
inﬁle=open(ﬁlename, 'x')!
!
except IOError:!
endofprogram=True!
!
***Reading: treat as list walk through list, start program using for loop for line in ﬁle. Get rid of
‘/n’ character at end of ﬁrst line. Check if its a blank line. check if it is not a comment.!
need TSV ﬁle!
strings are not mutable: to strip a string in a ﬁle it can me done as so:!
if endofprogram== False:\!
for line in inﬁle: !
line=line.strip('/n')!
#because strings are not mutable you have to make it into variable!
!
endofprogram= False!
highest=0!
!
try:!
ﬁlename=input('enter name of ﬁle: ')!
inﬁle=open(ﬁlename, 'y')!
!
except IOError:!
endofprogram=True!
!
if endofprogram== False:!
for line in inﬁle: !
line=line.strip('/n')!
#because strings are not mutable you have to make it into variable!
if (len(line)!=0) and (line[0] != '#') :!