Debugging, Escape Command

Download Report

Transcript Debugging, Escape Command

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)
>> 3.4900000000002
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!””)
print (“This class is \“Awesome!\””)
>> This class is “Awesome!”
#error!
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/
>> tree
/\
/ \
/
\
I I
\\ \n | | """)
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 = ( (5 + 10 + 20) / 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:
24

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.5
5 // 2
#2
5%2
# 1 (remainder from divisor of 2)
Converting Math Formulas into
Programming Statements
Most math formulas need to be converted into a
format that Python can understand
Examples:
10 x y
10 * x * y
( 3 ) ( 12 )
3 * 12
𝑦=
3𝑥
2
y=3*x/2
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 1
Operand 2
Result
int
int
int
float
float
float
float
int
float
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
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
 Good ol’ Precalc!
𝐴 = 𝐴0
𝑟
1+
𝑛
𝐴 = final price of the car
𝐴0 = initial price of the car
𝑟 = interest rate (in decimal form)
𝑛 = number of payments per year
𝑡 = number of years
𝑛𝑡