General Programming PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: General Programming


1
General Programming
  • Introduction to Computing Science and Programming
    I

2
General Programming
  • Basic set of steps for writing a program
  • Define the problem.
  • Create an algorithm to solve the problem and
    write it out in pseudocode
  • Convert this algorithm into actual code, testing
    small parts of it along the way
  • When finished converting the algorithm to code,
    test for errors
  • Debug any errors and go back to the previous step

3
General Programming
  • When you are finished writing your algorithm,
    look for pieces that you can program and test
    separately.
  • To illustrate the point, lets look at the second
    part of Lab 2 and see how we can break it apart

4
Example
  • Write a program that displays a menu to the user
    and based on their choice, calculates the area of
    a circle or triangle whos attributes are input
    by the user.
  • Algorithm
  • write menu to screen
  • read option
  • if option is triangle
  • read base/height from user
  • if base and height are gt 0
  • calculate and display area
  • else
  • display error
  • else if option is circle
  • read radius
  • if radius is gt 0
  • calculate and display circles area
  • else
  • display error
  • else
  • write incorrect option entered

5
Example
  • What parts of this algorithm can you code and
    test separately?
  • Menu Structure
  • Triangle calculation
  • Circle calculation
  • User input and check for negative values
  • Each of these could be coded and tested
    separately before combining.

6
General Programming
  • This idea of breaking the problem up may seem
    unimportant when working with the small programs
    we are now, but it is still helpful.
  • When you move on to more complex programs the
    idea is essential.
  • If you write large amounts of code with having
    made sure small pieces are correct, finding
    errors becomes more difficult.

7
Errors
  • You will encounter errors as you write your
    programs.
  • There are other ways to categorize them, but we
    will look at three basic types of error.
  • Syntax Errors
  • Runtime Errors
  • Semantic Errors

8
Syntax Errors
  • Errors in the syntax of your code.
  • Syntax refers to the rules that define proper
    statements.
  • Python will detect syntax errors and not allow
    you to run your code until you correct them.

9
Runtime Errors
  • Once your code has no syntactic errors, you are
    allowed to execute it
  • Runtime errors refer to errors that do not
    present themselves until your code is executed
    (runtime).
  • Examples
  • Division by zero.
  • Improper conversions.
  • Your program will be terminated if a runtime
    error occurs during execution.

10
Semantic Errors
  • Errors in the semantics of your code.
  • Syntax defines how you can create statements.
    Semantics tell you what the meaning of those
    statements are.
  • A program has semantic errors if it runs and
    completes, but does not do what it was created to.

11
Error Message
  • When an error occurs, Python prints some info to
    help you understand what happened. Learning how
    to understand these error message will help you
    solve problems in your programs.
  • For Example
  • gtgtgt 4/0
  • Traceback (most recent call last)
  • File "ltpyshell6gt", line 1, in ltmodulegt
  • 4/0
  • ZeroDivisionError integer division or modulo by
    zero
  • The last line tells you what type of error
    occurred.
  • The third line prints out the statement from that
    line.
  • The second line tells you on what line of code
    the error occurred. This is more helpful when
    executing a program.

12
Some Python Errors
  • ZeroDivisionError
  • NameError
  • Try to use a variable that doesnt exist.
  • ValueError
  • Try to convert an inappropriate string to a
    number

13
Correcting Errors
  • Strategies for correcting errors (debugging)
  • Remove parts of your code to see if they are
    involved in the error.
  • Add print statements to check the value of a
    variable that is involved. (remember to remove
    these statements when finished)

14
Coding Style
  • Coding style deals with how you format your code.
    Good coding style makes it easy for people to
    understand your code
  • Coding style issues.
  • Comments
  • Proper spacing
  • Good variable/function names

15
Comments
  • A comment is text you put in your code that will
    be ignored. This allows you to give short
    explanations to help people understand your code.
  • Use the character and anything written after it
    will be ignored

16
Comments
  • Dont add comments to explain every line
  • add one to x
  • x x1
  • Often useful to add a comment for chunks of code,
    such as before control structures
  • if the user entered good data, add it in
  • if value gt 0
  • sum sum value
  • count count 1
  • search for a value that divides num
  • while numfactor ! 0
  • factor factor 1

17
Spacing
  • Poor spacing can make lines of code more
    difficult to understand.
  • Bad y 100 / x1
  • Good y 100/x 1
  • Add blank lines in the code to make it easier to
    read.

18
Coding Style
  • Bad
  • a int(raw_input("Enter an integer "))
  • b 0
  • for i in range(a1)
  • if igt0
  • if a i0
  • b b 1
  • print "There are " str(b) " factors.
  • Good
  • get user input and initialize
  • num int(raw_input("Enter an integer "))
  • count 0
  • check every possible factor (1...num)
  • for factor in range(1, num1)
  • if factor divides num, it really is a factor
  • if numfactor 0
  • count count 1
Write a Comment
User Comments (0)
About PowerShow.com