Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Debugging, Escape Command, More Math

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

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

4 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! ’ )

5 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

6 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) >> 3.4900000000002

7 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

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

9 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!”

10 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

11 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!”)

12 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

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

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

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

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

17 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.5 5 // 2# 2 5 % 2 # 1 (remainder from divisor of 2)

18 Converting Math Formulas into Programming Statements

19 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

20 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 = 5 + 2 / 7 \ + 8 – 12  This also works for the print ( ) function

21 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!

22 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)

23 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!”

24 Practice: I woke up in a new Bugatti


Download ppt "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."

Similar presentations


Ads by Google