Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Introduction to C Programming
Programming Languages: Notes for Class Discussion: V Deena Engel’s class.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Python November 14, Unit 7. Python Hello world, in class.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
Introduction to Python
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC Programming I Lecture 6 September 4, 2002.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Python Let’s get started!.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
GCSE Computing: Programming GCSE Programming Remembering Python.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Few More Math Operators
CompSci 230 S Programming Techniques
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Topics Designing a Program Input, Processing, and Output
A Playful Introduction to Programming by Jason R. Briggs
Completing the Problem-Solving Process
Chapter 2 Assignment and Interactive Input
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variables, Expressions, and IO
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Few More Math Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to C++ Programming
Arithmetic Expressions & Data Conversions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Unit 3: Variables in Java
Data Types and Maths Programming Guides.
Arithmetic Expressions & Data Conversions
Introduction to Python
Presentation transcript:

Debugging, Escape Command, More Math

It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth year is from this information and report it back to the user.

Debugging  The process of debugging is really to find errors (bugs) and then to resolve them

Types of Errors  Syntax errors: the code does not follow the rules for the language; for example, a single quote is used where a double quote is needed, or a colon is missing, or a keyword is used as a variable name print ( “ Hello, world! ’ )

Types of Errors  Runtime errors: in this case, your code is fine but the program does not run as expected (it “crashes”). For example, if your program is meant to divide two numbers, but the divisor is zero, a runtime error will occur. var1 = float( input (‘give me a number:’) ) number = var1 + 4 >> give me a number: seventeen thirty eight # runtime error

Types of Errors  Logic errors: these can be the hardest to find. In this case, the program is correct from a syntax perspective, and it runs, but the result is not what you were looking for. For example, if your program prints “2 + 2 = 5” the answer is … clearly wrong. var = 3.49 print (var) >>

Example Logic Error num_1 = float ( input ( ‘ give me a number:’ ) ) num_2 = float ( input ( ‘ give me another number:’ ) ) Print ( ‘ the sum is: ’, num_1 – num_2 ) >> give a number: 43 give me another number: 32 the sum is: 11

Tips for debugging  Set small, incremental goals for your program. Don’t try and write a large program all at once.  Stop and test your work often.  Use comments to have Python ignore certain lines that are giving you trouble.  Ask a friend.

Escape Key “\”  The backslash ( “ \ ” ) is known as an escape key in Python  It tells Python that the character directly following the backslash will not function in it’s regular nature  Example: print (“This class is “Awesome!””) #error! print (“This class is \“Awesome!\””) >> This class is “Awesome!”

Examples of the Escape Key print (“We saw this \n this will print a new line”) >> We saw this this will print a new line print (""" /\\ \n / \\ \n/ \\ \n | | """) >> tree /\ / \ I I

Practice: Debugging # Name: Donald Seok # Title: “I don’t always make mistakes, but when I do …” first_name = ‘Donald” last_name = Seok print ( “Welcome to “Computer Programming” 101,”, first_name last_name, “!” ) print ( ) answer_1 = float ( input ( “What is the answer to 5 plus 2?” ) Print (“Your answer:”, answer_1) print ( )) real_answer = 5 * 2 printt (“If you answered”, real_answer, “you’re right!”)

Division Operations  Python contains two different division operators  The “/” operator is used to calculate the floating-point result of a division operation  The “//” operator is used to calculate the integer result of a division operation, it will throw away the remainder. *** This operation will always round DOWN. Examples: print ( 5 // 2 )# 2 print ( -5 // 2 ) # -3

Order of Operations  Python follows the order of operations (PEMDAS)  You can use parentheses inside your math expressions to group operations  Example: x = ( ( ) / 60 ) * 100

PEMDAS  Multiplication/Division, Addition/Subtraction must be done in order that it shows up, not interchangeable 3 * 4 / 2 * 5 (3 * 4) / (2 * 5)

Practice  Write a program that asks the user for their past three test scores.  Calculate the average score in a single variable and output it to the user.

Exponents  You can raise any number to a power by using the “ ** ” operator  Example: 2 4  2 ** 4

Remainder Operator (modulo)  The modulo operator “ % ” returns the remainder portion of a division operation  This is basically the opposite of the “ // ” operator  Examples: 5 / 2 # // 2# 2 5 % 2 # 1 (remainder from divisor of 2)

Converting Math Formulas into Programming Statements

Mixed Type Expressions  Python allows you to mix integers and floats when performing calculations  The result of a mixed-type expression will evaluate based on the operands used in the expression Operand 1Operand 2Result int float intfloat

Line Continuation  Sometimes expressions can get to be very long  You can use the “ \ ” symbol to indicate to Python that you’d like to continue the expression onto another line **  Example: x = / 7 \ + 8 – 12  This also works for the print ( ) function

Practice: Time Calculations  Ask the user to input a number of seconds as a whole number. Then express the time value inputted as combination of minutes and seconds >> Enter seconds: 110 That’s 1 minute and 50 seconds!

Practice: I woke up in a new Bugatti  In this exercise, you will ask the user for:  What is the price of the car (before interest)  An interest rate value (in decimal form)  How many years they want to pay it off for (monthly)

Practice: I woke up in a new Bugatti  Calculate the total cost of their car (after interest)  Calculate their monthly payment  Output a statement that says: “The final price of your car is _________. Your monthly payment will be ___________ for a term of __________ months. Enjoy your car!”

Practice: I woke up in a new Bugatti