Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Programming Introduction to Computing Science and Programming I.

Similar presentations


Presentation on theme: "General Programming Introduction to Computing Science and Programming I."— Presentation transcript:

1 General Programming Introduction to Computing Science and Programming I

2 General Programming Basic set of steps for writing a program Basic set of steps for writing a program Define the problem. Define the problem. Create an algorithm to solve the problem and write it out in pseudocode 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 Convert this algorithm into actual code, testing small parts of it along the way When finished converting the algorithm to code, test for errors When finished converting the algorithm to code, test for errors Debug any errors and go back to the previous step 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. When you are finished writing your algorithm, look for pieces that you can program and test separately. To illustrate the point, let’s look at the second part of Lab 2 and see how we can break it apart To illustrate the point, let’s 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 who’s attributes are input by the user. Write a program that displays a menu to the user and based on their choice, calculates the area of a circle or triangle who’s attributes are input by the user. Algorithm Algorithm write menu to screen read option if option is triangle read base/height from user if base and height are >= 0 calculate and display area else display error else if option is circle read radius if radius is >= 0 calculate and display circle’s area else display error else write incorrect option entered

5 Example What parts of this algorithm can you code and test separately? What parts of this algorithm can you code and test separately? Menu Structure Menu Structure Triangle calculation Triangle calculation Circle calculation Circle calculation User input and check for negative values User input and check for negative values Each of these could be coded and tested separately before combining. 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. 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. 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. 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. 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. There are other ways to categorize them, but we will look at three basic types of error. Syntax Errors Syntax Errors Runtime Errors Runtime Errors Semantic Errors Semantic Errors

8 Syntax Errors Errors in the syntax of your code. Errors in the syntax of your code. Syntax refers to the rules that define proper statements. 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. 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 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). Runtime errors refer to errors that do not present themselves until your code is executed (runtime). Examples: Examples: Division by zero. Division by zero. Improper conversions. Improper conversions. Your program will be terminated if a runtime error occurs during execution. Your program will be terminated if a runtime error occurs during execution.

10 Semantic Errors Errors in the semantics of your code. Errors in the semantics of your code. Syntax defines how you can create statements. Semantics tell you what the meaning of those statements are. 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. 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. 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 For Example >>> 4/0 Traceback (most recent call last): File " ", line 1, in File " ", line 1, in 4/0 4/0 ZeroDivisionError: integer division or modulo by zero The last line tells you what type of error occurred. The last line tells you what type of error occurred. The third line prints out the statement from that line. 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. 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 ZeroDivisionError NameError NameError Try to use a variable that doesn’t exist. Try to use a variable that doesn’t exist. ValueError ValueError Try to convert an inappropriate string to a number Try to convert an inappropriate string to a number

13 Correcting Errors Strategies for correcting errors (debugging) Strategies for correcting errors (debugging) Remove parts of your code to see if they are involved in the error. 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) 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 deals with how you format your code. Good coding style makes it easy for people to understand your code Coding style issues. Coding style issues. Comments Comments Proper spacing Proper spacing Good variable/function names 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. 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 Use the # character and anything written after it will be ignored

16 Comments Don’t add comments to explain every line Don’t add comments to explain every line #add one to x x = x+1 Often useful to add a comment for chunks of code, such as before control structures 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 >= 0: sum = sum + value count = count + 1 # search for a value that divides num while num%factor != 0: factor = factor + 1

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

18 Coding Style Bad Bad a = int(raw_input("Enter an integer: ")) b = 0 for i in range(a+1): if i>0: if a % i==0: b = b + 1 print "There are " + str(b) + " factors.“ Good 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, num+1): # if factor divides num, it really is a factor if num%factor == 0: count = count + 1 # output results print "There are " + str(count) + " factors."


Download ppt "General Programming Introduction to Computing Science and Programming I."

Similar presentations


Ads by Google