rec11 - CS 177 Week 11 Recitation Slides Writing out...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS 177 Week 11 Recitation Slides Writing out programs, Reading from the Internet and Using Modules
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 ANY QUESTIONS?
Background image of page 2
Writing a program to write programs First, a function that will automatically change the text string that the program “littlepicture” draws As input, we’ll take a new filename and a new string. We’ll find () the addText , then look for the first double quote, and then the final double quote. Then we’ll write out the program as a new string to a new file 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
changeLittle("sample.py","Here is a sample of changing a program") 4 Original: def littlepicture(): canvas=makePicture(getMediaPat h("640x480.jpg")) addText(canvas,10,50,"This is not a picture") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,5 00,yellow) addRect(canvas,10,210,290,490) return canvas Modified: def littlepicture(): canvas=makePicture(getMediaPat h("640x480.jpg")) addText(canvas,10,50,"Here is a sample of changing a program") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,5 00,yellow) addRect(canvas,10,210,290,490) return canvas
Background image of page 4
Changing the little program automatically 5 def changeLittle(filename,newstring): # Get the original file contents programfile=r"C:\Documents and Settings\Mark Guzdial\My Documents\py- programs\littlepicture.py" file = open(programfile,"rt") contents = file.read() file.close() # Now, find the right place to put our new string addtext = contents.find("addText") firstquote = contents.find('"',addtext) #Double quote after addText endquote = contents.find('"',firstquote+1) #Double quote after firstquote # Make our new file newfile = open(filename,"wt") newfile.write(contents[:firstquote+1]) # Include the quote newfile.write(newstring) newfile.write(contents[endquote:]) newfile.close()
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

rec11 - CS 177 Week 11 Recitation Slides Writing out...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online