Error Practice: Identifying and Fixing Common Programming Errors, Assignments of Computer Science

Examples of common programming errors in jython and explains how to identify and fix them. Errors include name errors, syntax errors, type errors, logical errors, and out-of-bounds errors. Each example includes the broken code, the error message, and instructions for fixing the error.

Typology: Assignments

Pre 2010

Uploaded on 08/04/2009

koofers-user-502
koofers-user-502 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ERROR PRACTICE
WHAT TO LOOK FOR:
A) A local or global name could not be found
*Check to make sure you have defined all variables before they are used.
B) Your code is not legal Jython.
*Look for syntax errors, like indentations, missing parentheses and colons, and equal
signs that are either needed or unneeded (if statements and setting variables, = vs.
==).
C) An attempt was made to call a function with a parameter of an invalid type.
*Remember what needs to be input into each function.
pickAFile()
makePicture(picture)
getRed(pixel)
setRed(pixel, redamount)
getColor(pixel)
setColor(pixel, color)
return picture
distance(color, color)
makeColor(red, green, blue)
D) It’s a logical error. (It runs but doesn’t do what you intended it to do)
*Common ones are returns inside a loop, no returns, prints instead of returns; trace
to see what the function does.
E) It’s an out-of-bounds error.
*Does the range start with 0 for a pixel? Pixels start at 1,1. Are the numbers too big
for the picture or canvas? Then it would have no pixel there.
pf3
pf4
pf5

Partial preview of the text

Download Error Practice: Identifying and Fixing Common Programming Errors and more Assignments Computer Science in PDF only on Docsity!

ERROR PRACTICE

WHAT TO LOOK FOR:

A) A local or global name could not be found *Check to make sure you have defined all variables before they are used. B) Your code is not legal Jython. *Look for syntax errors, like indentations, missing parentheses and colons, and equal signs that are either needed or unneeded (if statements and setting variables, = vs. ==). C) An attempt was made to call a function with a parameter of an invalid type. *Remember what needs to be input into each function. pickAFile() makePicture(picture) getRed(pixel) setRed(pixel, redamount) getColor(pixel) setColor(pixel, color) return picture distance(color, color) makeColor(red, green, blue) D) It’s a logical error. (It runs but doesn’t do what you intended it to do) *Common ones are returns inside a loop, no returns, prints instead of returns; trace to see what the function does. E) It’s an out-of-bounds error. *Does the range start with 0 for a pixel? Pixels start at 1,1. Are the numbers too big for the picture or canvas? Then it would have no pixel there.

  1. def makeSnazzy(picture): for x in range(1, getWidth(picture) + 1): for y in range(1, getHeight(picture) + 1): p = getPixel(picture, x, y) setBlue(p, 255) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) An attempt was made to call a function with a parameter of an invalid type. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it

  1. def makeSnazzy(picture): for x in range(1, getWidth(picture) + 1) for y in range(1, getHeight(picture) + 1): setBlue(p, 255) p = getPixel(picture, x, y) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) An attempt was made to call a function with a parameter of an invalid type. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it

  1. def mixColors(picture): for x in range(getWidth(picture) + 1): for y in range(getHeight(picture) + 1): px = getPixel(picture, x, y) b = getBlue(px) r = getRed(px) setBlue(px, r) setRed(px, b) setGreen(px, getGreen(px)*.5) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) The parameter for a function called is incorrect. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it

  1. def mixColors(picture): for x in range(1, getWidth(picture) + 1): for y in range(1, getHeight(picture) + 1): b = getBlue(px) r = getRed(px) setBlue(px, r) setRed(px, b) setGreen(px, getGreen(px)*.5) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) The parameter for a function called is incorrect. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it

  1. def mixColors(picture): for x in range(1, getWidth(picture) + 1): for y in range(1, getHeight(picture) + 1): px = getPixel(picture, x, y) b = getBlue(px) r = getRed(px) setBlue(px, r) setRed(px, b) setGreen(px, getGreen(px)*.5) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) The parameter for a function called is incorrect. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it

  1. def mixColors(picture): for x in range(1, getWidth(picture) + 1): for y in range(1, getHeight(picture) + 1): px = getPixel(picture, x, y) b = getBlue(px) r = getRed(px) setBlue(px, r) setRed(px, b) setGreen(px, getGreen(px)*.5) print picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) The parameter for a function called is incorrect. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error.

Scratch out the broken line and fix it

  1. def mixColors(picture): for x in range(1, getWidth(picture) + 1): for y in range(1, getHeight(picture) + 1): px = getPixel(picture, x, y) b = getBlue(px) r = getRed(px) setBlue(px, b) setRed(px, r) setGreen(px, getGreen(px)*.5) return picture You loaded it and then you did the following in the command area:

f = pickAFile() picture = makePicture(f) makeSnazzy(picture) Oh no! It gives you an error! What error did you get? A) A local or global name could not be found B) Your code is not legal Jython. C) The parameter for a function called is incorrect. D) It’s a logical error. (It runs but doesn’t do what you intended it to do) E) It’s an out-of-bounds error. Scratch out the broken line and fix it ANSWERS: 1. D 2. B 3. C 4. A 5. E 6. A 7. B 8. D 9. C 10. B 11. D