 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Midterm Exam 1 Review Spring 2004: Tracing the printing
(Return to Sp2004 Midterm 1 Review)
Questions, answers, comments on answers, comments on questions?
Amelia Cipolla
This is what I saved and loaded in the top part of jes:
def newfunction(a,b,c):
print a
list1=range(1,5)
value=0
for x in list1:
print b
value=value+1
print c
print value
which I saved as newFunction.py
But when I typed in the command area:
newFunction("I", "you", "walrus")
I got this error message:
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
WHATS WRONG? Why am I unable to print ANYTHING when I enter it in exactly as written???
| Did you load the program? Simple mistakes like that cause it to print out that error. REMOVED again! Kelly Farrell |
I read through the recipe myself and got the following:
I
you
you
you
you
walrus
4
but, "you" was printed 5 times on JES and the #5 was put there instead of 4
please help!- Kyla REMOVEDCroy
| REMOVED your program, Kyla. Did you type it exactly the way it is here? I get the same result from JES that you got by tracing it. (BTW, BRAVO! Tracing it yourself is the way to learn it!) Mark Guzdial |
Could you please explain "get value?" I don't really understand why it wouldn't just be zero, especially since it said in the code that value = 0?
Student1310
Okay, from what I've gotten from other postings...our range in the program is given to be (1,5) which goes (1,2,3,4) and doesn't include 5..like hte 255 pixels thing (when there's 256). Anyways, you start with 0 because it tells you that value=0, BUT...then the program says that the value=value+1...so, you'd go 0+1 =1,1+1=2,2+1=3..and so forth and end when you get the value 4..so that's how many times you say "you" and that's your new value...am I right TA's/profs? :) Student1113
The reasons seem to be inconsistent to me. In the previous tracing problem the value seemed to be given by what value was at the end of the loop (10-4)= 6 but now it just seems to be the final value of the range or the number of times you was printed. Can someone explain the difference? Is it different because in once case we added and the other we subtracted? Student998
Hey Amelia, you named your function newfuction(a, b, c) and you said you typed in the command area newFunction("I", "you", "walrus"). The capitalization is different, which makes a big difference. Kristin Noell
def newfunction(a, b, c):
print a
list1 = range(1,5)
value = 0
for x in list1:
print b
value = value + 1
print c
print value
call
newfunction("I", "you", "walrus")
print "I"
list1 = range(1,2,3,4)
value = 0
for x in list1:
print "you"
value = value + 1
- value = 0+1 = 1
- value = 1+1 = 2
- value = 2+1 = 3
- value = 3+1 = 4
print "walrus"
print 4
| Whoever spelled out the above: NICE JOB! The not including "5" has nothing to do with not including "256." There isn't a hard-and-fast rule, Melanie – you just have to trace the program (like the above) to figure out what happens. Mark Guzdial |
I don't understand why values for a, b, and c aren't being printed; it looks like it's just one number, and with colors, there are RGB values that give back numbers for RGB. In this case, since you're asking JES to print a, b, AND c, shouldn't you have (#for a, # for b, #for c)? Am I missing a big concept here? Student1192
The way I understand it, a, b, and c don't have to be numbers. They can be whatever you want (hence using words). So if you decide you want "googlesplotch" to be in the space of a, then it will replace a in every instance. The function gives you a value to start with in your for loop. The for loop computes a 'number' or value each time the loop goes. [which in this case is 4 times]. Not only does it compute a value, but it asks to print whatever is in the b slot each time. For this program "you" is in the b slot so that gets printed. So, it says print a (which is now I), then print b (which is now you) and after you print that increase our value by 1. Then that loop repeats whichever amount of times (4 in our case). Then at the end it says print c (which is now walrus) and then print value (the final value after your for loop). So you get:
I
you
you
you
you
walrus
4
{Hey everyone I know that was long and pretty crazy sounding, but I think it was REMOVED for myself.. and hopefully it will help someone else} Jennifer Garrett
Thanks!! Student1192
I am the walrus.
| Goo goo ga joob. Mark Guzdial (Yay, Jennifer! Nice explanation!) |
Link to this Page