Introduction to Computing Science and Programming I

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Advertisements

Introduction to Computing Science and Programming I
Debugging Introduction to Computing Science and Programming I.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 3 Planning Your Solution
The University of Texas – Pan American
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
General Programming Introduction to Computing Science and Programming I.
Computer Science 101 Introduction to Programming.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
1 Program Planning and Design Important stages before actual program is written.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Few More Math Operators
Development Environment
Exceptions in Python Error Handling.
CMPT 120 Topic: Python’s building blocks -> More Statements
Pseudocode and comments
Topics Designing a Program Input, Processing, and Output
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to Programming and Visual Basic
Lecture 1 Introduction Richard Gesick.
Introduction to Python
Completing the Problem-Solving Process
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Chapter 2 Assignment and Interactive Input
Variables, Expressions, and IO
Lecture 2 Introduction to Programming
Lecture 07 More Repetition Richard Gesick.
Algorithm and Ambiguity
Computer Science 101 While Statement.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture 4B More Repetition Richard Gesick
Ruth Anderson UW CSE 160 Winter 2017
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Topics Introduction to File Input and Output
Number and String Operations
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Programming Fundamentals (750113) Ch1. Problem Solving
Ruth Anderson UW CSE 140 Winter 2014
Chapter 3 – Control Structures
ERRORS AND EXCEPTIONS Errors:
Problem Solving Skill Area 305.1
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
Python programming exercise
Programming Fundamentals (750113) Ch1. Problem Solving
Topics Designing a Program Input, Processing, and Output
Tonga Institute of Higher Education IT 141: Information Systems
Programming Fundamentals (750113) Ch1. Problem Solving
An Introduction to Debugging
Topics Designing a Program Input, Processing, and Output
Tonga Institute of Higher Education IT 141: Information Systems
Loops.
Unit 3: Variables in Java
Pseudocode and comments
Chopin’s Nocturne.
Review of Previous Lesson
Topics Introduction to File Input and Output
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Introduction to Computing Science and Programming I General Programming Introduction to Computing Science and Programming I

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

General Programming 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

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. 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 write incorrect option entered

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.

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.

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

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.

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.

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.

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 >>> 4/0 Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> 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.

Some Python Errors ZeroDivisionError NameError ValueError Try to use a variable that doesn’t exist. ValueError Try to convert an inappropriate string to a number

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)

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

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

Comments 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 # 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

Spacing 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.

Coding Style Bad Good 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 # 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."