Runtime errors

Download Report

Transcript Runtime errors

REVIEW
No curveballs this time …
PROBLEM TYPE #1:
EVALUATIONS
Evaluate
Expression
5–2
5 ** 2
“5” + “2”
“5” * 2
5/2
5 // 2
5%2
5 + 2.0
5.0 * 2.0
Output
Data Type
Evaluate
Expression
Output
Data Type
5–2
5 ** 2
“5” + “2”
“5” * 2
3
25
52
55
integer
integer
string
string
5/2
5 // 2
5%2
2.5
2
1
float
integer
integer
5 + 2.0
5.0 * 2.0
7.0
10.0
float
float
Evaluate
Expression
10 > 2
2<1
2 != 4
4 == 2 ** 2
‘cat’ < ‘dog’
‘cat’ < ‘Dog’
5 > 2 and 10 > 11
5 > 2 or 10 > 11
5 > 2 and not (10 > 11)
Output
Data Type
Evaluate
Expression
Output
Data Type
10 > 2
2<1
2 != 4
4 == 2 ** 2
True
False
True
True
Boolean
Boolean
Boolean
Boolean
‘cat’ < ‘dog’
‘cat’ < ‘Dog’
5 > 2 and 10 > 11
True
False
False
Boolean
Boolean
Boolean
5 > 2 or 10 > 11
5 > 2 and not (10 > 11)
True
True
Boolean
Boolean
Evaluate
Expression
str.lower(“HeLLo”)
str.upper(‘HeLLo’)
format(5.1, ‘.2f’)
format(5.1, ‘10.2f’)
format(5, ‘10d’)
random.randint(1,5)
len(‘cat’)
len(‘cat’ + ‘dog’)
not (5 > 2 and 5 < 4 )
Output
Data Type
Evaluate
Expression
Output
Data Type
str.lower(“HeLLo”)
str.upper(‘HeLLo’)
format(5.1, ‘.2f’)
format(5.1, ‘10.2f’)
hello
HELLO
5.10
string
string
string
string
format(5, ‘10d’)
random.randint(1,5)
len(‘cat’)
len(‘cat’ + ‘dog’)
not (5 > 2 and 5 < 4 )
5.10
5
{1, 2, 3, 4, 5}
3
6
True
string
integer
integer
integer
Boolean
PROBLEM TYPE #2:
SHORT ANSWER
Short Answer – Sample Question
◦ Explain what a Boolean expression is and why you would use one
while programming. Give an example in a short code.
Short Answer – Sample Question
◦ Explain what a Boolean expression is and why you would use one
while programming. Give an example in a short code.
Boolean expressions are conditional statements that compare two
or more different values, using an operator (and, or, not, >, <, >=,
<=, ==)
These expressions always hold a value of either True or False
They are normally seen as the conditions of an “IF” statement
Short Answer – Sample Question
◦ Identify two different functions that convert one data type to
another. Then use each in an example code and explain when
you would use it in an actual program.
Short Answer – Sample Question
◦ Identify two different functions that convert one data type to
another. Then use each in an example code and explain when
you would use it in an actual program.
str(argument) – converts any data type into a string literal
int(argument) – converts an argument into an integer, but it must
be given that it CAN be converted into an integer, (strings will
cauase an error), for floats, it will always round the number down
float(argument) – converts an argument into a float, given that
the argument CAN be converted into a float
Short Answer – Sample Question
◦ Explain how the “and” and “or” operators work. Write a short
code to show an example of each and describe how you would
use it in an actual program.
Short Answer – Sample Question
◦ Explain how the “and” and “or” operators work. Write a short
code to show an example of each and describe how you would
use it in an actual program.
The “and” operator checks for two conditions simultaneously and
both conditions must hold True in order for the Boolean expression
to hold True.
The “or” operator checks for two conditions as well, but only one
of them needs to hold True in order for the expression to hold True.
It can be though of as checking one of two conditions.
Short Answer – Sample Question
◦ What’s the difference between using an “ELIF” statement and a
nested decision structure? Describe both individually and explain
the difference between them.
Short Answer – Sample Question
◦ What’s the difference between using an “ELIF” statement and a
nested decision structure? Describe both individually and explain
the difference between them.
An ELIF statement is in essence the same thing as a nested
decision structure. All ELIF statements can be written using a
nested structure, ELIF’s are purely for convenience. However, they
are very convenient because an ELIF statement extends a single
decision structure while, any time an IF statement is written, you
create a new decision structure, whether nested or not.
PROBLEM TYPE #3:
TRACE THE OUTPUT
Trace the Output #1
Trace the Output #1
Trace the Output #2
Trace the Output #2
Trace the Output #3
Trace the Output #3
Trace the Output #4 …(1of 3)
This problem was made
by a fellow student. I
figured I would include it
here because it serves as
good practice.
This is only the beginning
… see next slide for
continuation.
Trace the Output #4
…(2 of 3)
Trace the Output #4 …(3 of 3)
PROBLEM TYPE #4:
DEBUGGING
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
Types of Errors
◦ Please pay close attention this time around to naming the type of
error that is being made
◦ Realize the most common error is a syntax error, at least when
looking at the source code
Example:
x=3
print(x + “4”)
# this is considered a syntax error because you
# are trying to add an integer to a string
PROBLEM TYPE #5:
WRITE A PROGRAM
Write a program that …
◦ Asks the user for two random words.
◦ Sort the words in alphabetical order and print them out.
◦ Sort the words in size order and print them out.
Write a program that …
Write a program that …
◦ Has the computer virtually roll two 6-sided dice.
◦ Output the result as follows:
Virtual Dice Roll: 3 and 5
Write a program that …
Write a program that …
◦ Allows the user to input their last three test grades.
◦ Calculate and print out their final average.
◦ Then, print out their letter grade.
Assume:
A:
90 ≤ 𝑥 ≤ 100
B:
80 ≤ 𝑥 < 90
C:
70 ≤ 𝑥 < 80
D:
65 ≤ 𝑥 < 70
F:
𝑥 < 65
Write a program that …
Write a program that …
◦ Allows the user to qualify for a credit card.
Requirements:
- They must make at least $30,000 a year (disregard
tax/deductions)
- Their rent payment cannot be more than 50% of their total
monthly salary
- However, if they own their house, you do not need to take their
monthly rent into account
Credit Card Qualifier: Sample Runs
Credit Card Qualifier
Annual Income: 40000
Do you own your house? (y/n): y
You qualify!
Credit Card Qualifier
Annual Income: 40000
Do you own your house? (y/n): n
Monthly Rent: 1200
You qualify!
Credit Card Qualifier
Annual Income: 50000
Do you own your house? (y/n): n
Monthly Rent: 4000
Sorry, you do not qualify!
Write a program that …
Write a program that …
◦ Allows a user to input the price of 3 items they are purchasing in a
store
◦ Then, calculate and print out the subtotal
◦ Calculate the tax (assume 7%)
◦ Finally, print out the total bill
◦ You should format your numbers for money-talk
Write a program that …
Write a program that …
◦ Allows a user to type in a 3 digit integer (assume, that the last
digit will not be zero)
◦ Print out the original number
◦ Then, print out the number in reversed order
Please enter a three digit number: 347
You entered: 347
347 backwards is 743
Write a program that …
Write a program that …
◦ Asks the user to type in a 2 digit integer (assume, that the last digit
will not be zero), and call it variable x
◦ Then, figure out variable y, which is number with the same digits
as x but reversed order
◦ Then, print out x and y and their sum
Please enter a two digit number: 25
25 reversed is 52
25 + 52 is 77
Write a program that …
BEST OF LUCK!