Download More Strings, File Processing and Methods and Functions - Notes | CSSE 120 and more Study notes Software Engineering in PDF only on Docsity!
MORE STRINGS,
FILE PROCESSING, AND
METHODS VS. FUNCTIONS
CSSE 120 – Rose-Hulman Institute of Technology
Outline
Class exercise: processing with lists
String representation
More on string input and output
File processing
Coming attraction: Objects
In-class practice time
The Software Development Process
Analyze the Problem
Determine Specifications
Create a Design
Implement the Design
Test/Debug the Program
Maintain the Program
Phases of Software Development
Analyze: figure out exactly what the problem to be
solved is
Specify: WHAT will program do? NOT HOW.
Design: SKETCH how your program will do its work,
design the algorithm
Implement : translate design to computer language
Test/debug : See if it works as expected.
bug == error, debug == find and fix errors
Maintain : continue developing in response to needs
of users
Consistent String Encodings
Needed to share data between computers
Examples:
ASCII—American Standard Code for Info. Interchange ―Ask-ee‖ Standard US keyboard characters plus ―control codes‖ Extended ASCII encodings Add various international characters Unicode Tens of thousands of characters Nearly every written language known
Class Exercise: Encoding
Download encode.py from Angel:
Lessons Modules to Download in Class Session 5 encode module
Verify that the module works on your computer:
Run the module Enter the message: Spam Enter the key: 1 Expected result: [84, 113, 98, 110]
Answer In-class Quiz questions 4 and 5
String Formatting
The % operator is overloaded
Multiple meanings depending on context
What does it mean for numbers?
Other meaning for %
Plug values from tuple into ―slots‖ in string Slots given by format specifiers
Format Specifiers
Syntax:
%.
Width gives total spaces to use
0 means as many as needed 0 n means pad with leading 0s to ntotal spaces - n means ―left justify‖ in the n spaces
Precision gives digits after decimal point
TypeChar is:
f for float, s for string, or d for decimal (i.e., int)
Open file: file_to_read = open('grades.txt', ' r ')
Read file:
.read() Returns one BIG string .readline() Returns next line, including \n .readlines() Returns BIG list of strings, 1 per line for in Iterates over lines efficiently
Close file: file_to_read.close()
Up Next: Objects
Why do we apply some operations like this:
infile = open('file.txt','r') abs(-1.2)
and others like this:
infile.read() circle.draw(win)
Files and circles are objects —data plus operations
Tells object to do something
Practice
Hand in quiz
Start working on HW
Practice with string formatting Practice with file processing