Debugging Some helps on A3 - University of Calgary in Alberta
Transcript of Debugging Some helps on A3 - University of Calgary in Alberta
CPSC 231Introduction to Computer Science for
Computer Science Majors I
YOUR NAME
DebuggingSome helps on A3
Debugging
What is a bug? What is debugging? A bug is a flaw that causes a program to produce an incorrect or
unexpected result, or to behave in unintended ways.
Debugging is finding and reducing the number of bugs, thus making a program behave as expected.
Avoid bugs that don’t exist: Be user friendly!
Sometimes not being user friendly causes the user to think the program has a bug.
Run input_debug.py. What do you think?
Now look at the code. What do you see?
Debugging syntax errors
Sometimes finding syntax errors can be hard. Python does not always provide reliable information about the
exact location of syntax errors.
However, the rough location is mostly correct.
How to debug then?
A general debugging solution is to use commenting. Comment the code sections that may have produced the error.
Reduce the comments gradually until you find the source of the error.
Using commenting for debug purposes
Review: Commenting in Python Anything after # is a comment until you reach end of the line.
You can use multi-line strings as multi-line comments.
Multi-line strings start and end with """ (three double quotes).
Example: Using commenting to debug syntax.py.
print(x)x = int(input("Enter a number: ")if (x > 0):elif (x > 10):elif (x > 100):elif (x > 1000):
Example: branches.py
Look at branches.py How do figure out if it’s
correct?
How to debug?
…
if (a > b):
if (x > 0) and (y > 0):
s1 = "miley"
elif (x > 10):
s2 = "sheen"
elif (y > z):
s3 = "twerp"
else:
s4 = "palin"
elif (a < b):
if (x > 0) or (y > 0) or (z > 0):
s5 = "min met"
elif (x > 0) or (y > 0) or (z > 0):
s5 = "max met"
else:
s6 = "no one could possible need
more than 640k RAM"
import random
a = int(input("Enter a number: "))
b = int(input("Enter a number: "))
x = random.randrange(1,1000001)
y = random.randrange(1,10000001)
z = random.randrange(1,10000001)
…
Example: branches_debug.py
Step 1: Find out which branch is evaluated as true. You can print debug information inside each branch.
import random
a = int(input("Enter a number: "))
b = int(input("Enter a number: "))
x = random.randrange(1,1000001)
y = random.randrange(1,10000001)
z = random.randrange(1,10000001)
print('before')
if (a > b):
print('1')
if (x > 0) and (y > 0):
s1 = "miley"
print('2')
elif (x > 10):
s2 = "sheen"
print('3')
elif (y > z):
s3 = "twerp"
print('4')
else:
s4 = "palin"
print('5')
elif (a < b):
print('6')
if (x > 0) or (y > 0) or (z > 0):
s5 = "min met"
print('7')
elif (x > 0) or (y > 0) or (z > 0):
s5 = "max met"
print('8')
else:
s6 = "no one could possible need
more than 640k RAM"
print('9')
print('after')
Example: branches_debugV2.py
Step 2: Display the contents of the branch controls. Given these values displayed, you can predict which branch
should execute.
import random
a = int(input("Enter a number: "))
b = int(input("Enter a number: "))
x = random.randrange(1,1000001)
y = random.randrange(1,10000001)
z = random.randrange(1,10000001)
print('before')
print("a=%d b=%d" %(a,b))
print("x=%d y=%d z=%d" %(x,y,z))
if (a > b):
print('1')
if (x > 0) and (y > 0):
s1 = "miley"
print('2')
elif (x > 10):
s2 = "sheen"
print('3')
elif (y > z):
s3 = "twerp"
print('4')
else:
s4 = "palin"
print('5')
elif (a < b):
print('6')
if (x > 0) or (y > 0) or (z > 0):
s5 = "min met"
print('7')
elif (x > 0) or (y > 0) or (z > 0):
s5 = "max met"
print('8')
else:
s6 = "no one could possible need
more than 640k RAM"
print('9')
print('after')
Example: loops.py
Look at loops.py. Which loops run? Which ones are endless? Which ones never
run?
import random
SIZE = 150
i = 1
while (i <= SIZE):
grade = random.randrange(-100,200)
while (grade < 0) or (grade > 100):
grade = random.randrange(-100,200)
id = random.randrange(0,100000000)
while (id <= 0) and (gpa >= 99999999):
id = random.randrange(0,100000000)
print("ID #%d\t%dgrade" %(id,grade))
Example: loops_debug.py
We can print debug information inside loop bodies.
import random
SIZE = 150
i = 1
print('Before loops')
while (i <= SIZE):
print('\tLoop 1')
grade = random.randrange(-100,200)
print('\tBefore loop 2')
while (grade < 0) or (grade > 100):
print('\t\tLoop 2')
grade = random.randrange(-100,200)
id = random.randrange(0,100000000)
print('\tBefore loop 3')
while (id <= 0) and (gpa >= 99999999):
print('\t\tLoop 3')
id = random.randrange(0,100000000)
print("ID #%d\t%dgrade" %(id,grade))
Programs that help you on A3
Look at the source code of this week's tutorials.
You will find a directory named assign_like, which contains some programs that will help you in writing assignment 3.
Working with ASCII characters.
Piping / output redirection.
Using command line arguments.
Reading from the input until the EOF character is reached.
ascii.py
Converts characters to ASCII codes
Converts ASCII codes to characters
# Character to code
ch = input("Enter a single character: ")
code = ord(ch)
print("Character...%s\tASCII code...%d" %(ch,code))
# Code to character
code = int(input("Enter an ASCII code: "))
ch= chr(code)
print("ASCII code...%d\tCharacter is...%s" %(code,ch))
Command line arguments
Go to this week’s ‘tutorials’ directory and run these commands: ls
ls -l
ls -a -l
Navigate to /home/uga and run these commands: ls
ls | more (piping)
ls > output.txt (output redirection)
ls > [your_home_directory]/output.txt
commandLine.py
Reads arguments from command line.
Prints the arguments and their order on the screen.
count = 1
for arg in sys.argv:
print("Arg #%d: %s" %(count,arg))
count = count + 1
commandLineUsage.py
Checks the number of provided arguments. len() function
If enough arguments are not provided, exits from the program and returns an error value. exit() function
PROBLEM = -1
if (len(sys.argv) < 2):
print("Usage: python <program name> <one or more arguments>")
exit(PROBLEM)
else:
count = 1
for arg in sys.argv:
print("Arg #%d: %s" %(count,arg))
count = count + 1
displaySentences.py
Reads from input until the EOF character is reached.
EOF = chr(4)
def getInput():
try:
ret = input()
except EOFError:
ret = EOF
return ret
###############################
# Main / start of program #
###############################
line = getInput()
sentence_count = 0
while (line != EOF):
sentence_count = sentence_count + 1
print("Sentence #%d: %s" %(sentence_count,line))
line = getInput()
Redirecting input and output
Redirecting input:
python3 displaySentences.py < data.txt
Redirecting input and output:
python3 displaySentences.py < data.txt > results.txt