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
Sadaf Ahmed
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-defined task!
return with nothing beside it means return none, the same is with out having return at all!
local variable is defined inside a function, scope is limited inside the function it is defined in*.!
local variable lives and dies with the function it is defined in!
defining 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 defined 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 flowed by space followed by name: ‘)!
#info split into two separate variable names!
name,grade=info.split()!
!
#putting info in tuple data aspect with two structures!
record=(name, float(grade))!
#record[0]= <name>!
#record[1]= <grade>!
!
accessing file name!
!
MARCH 11. 2014!
opening file object >> fileVariable=open(filename, mode)!
in read, append or write mode.!
3 ways of reading file, read characters, read line, use for loop.!
Good idea to put input of file 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 file exists already.!
to check if it exists : import>> import os.path!
using write function >> outfile.write (“rejerjhwr /n”)!
You're Reading a Preview

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 file, 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. outfile. write (‘words /n’)!
outfile.write (‘morewords /n’)!
integer into a string on slides*!
ie how to open a file!
!
endofprogram= False!
!
try:!
filename=input('enter name of file')!
infile=open(filename, 'x')!
!
except IOError:!
print ('error reading file')!
endofprogram=True!
!
***Reading: treat as list walk through list, start program using for loop for line in file. Get rid of
‘/n’ character at end of first line. Check if its a blank line. check if it is not a comment.!
need TSV file!
strings are not mutable: to strip a string in a file it can me done as so:!
if endofprogram== False:\!
for line in infile: !
line=line.strip('/n')!
#because strings are not mutable you have to make it into variable!
!
FORMAT FOR READING FILE:!
endofprogram= False!
highest=0!
!
try:!
filename=input('enter name of file: ')!
infile=open(filename, 'y')!
!
except IOError:!
print ('error reading file')!
endofprogram=True!
!
if endofprogram== False:!
for line in infile: !
line=line.strip('/n')!
#because strings are not mutable you have to make it into variable!
if (len(line)!=0) and (line[0] != '#') :!
name,grade=line.split('/t')!
You're Reading a Preview

Unlock to view full version