Midterm Exam 1 Review Spring 2003: What gets printed
Try the problem here from Sp 2003 Midterm Review #1
I went through the program and got my answers, but I couldn't test it because the program didn't work in JES. I know it says don't do it in JES, but I just wanted to check to see if my answers were right. Here is what I think it would be:
Hello back to you!
Hello
Hello
Hello
Hello
Hello
5
Hello back to you!
Is this right?
| Nearly. Count your Hello's again, and remember range()'s peculiarity about the last number. Mark Guzdial |
so, there should be 4 hellos and the number should be 6. is that right?
| Spell it out. And please, sign your name, so that we know who we're talking to. Mark Guzdial |
Hello back to you!
Hello
Hello
Hello
Hello
5
Hello back to you!
I believe this is what the prog should give you
David Petersen
This is what i got?
Hello back to you!
Hello
Hello
Hello
Hello
6
Hello back to you!
Michelle
That is what I got too Michelle!
Emily
Here's what I get:
Hello back to you!
Hello
Hello
Hello
Hello
Hello
0
Hello back to you!
Is this right?
Rebecca Phillips
I started with the value of 10 and assumed that the program printed "Hello" every time the range was 1 through 5. The last value I had was 1 and 1-1 equals 0. So, I figured it would print 0. However, I'm now thinking that maybe it prints 10 because "print value" isn't indented under the "for" loop. This is what I have:
Hello back to you!
Hello
Hello
Hello
Hello
Hello
10
Hello back to you!
Is this closer? Were Michelle and Emily right? Am I thinking about this correctly?
Rebecca Phillips
I understand all the other parts of this problem except for the value = value - 1 part. Why wouldn't it be 10-1=9 'cuz we assigned value to be 10 earlier in the code.
Becky Cocos
The only part I don't get is why the number is 6? Can someone please explain?
Lindsey Martin
| "print value" isn't inside the loop, but value = value - 1 is. We subtract one from value each time through the the loop. Mark Guzdial |
Who's right? or at least point out who's wrong. Please... Thanks!
Rebecca Phillips
| I just pointed out what was wrong. It's up to you to figure out which one above is right and which one is wrong. I don't post the answers. Mark Guzdial |
I know you don't post the answers, but you said earlier the same thing that I said about the "print value". That's the only comment I have about my last posting that I had with my answers. I just want to know if my thinking is right. I believe mine is correct. I can't understand how the others came up with 6 or how they got only four "Hello"'s. Since "print value" isn't inside the loop, the last value printed must be 10. However, you haven't said anything in reference to my five "Hello"'s. You said something in the first posting about them; however, I don't understand what you were meaning. Emily, Michelle could you tell me how you were doing it?
Rebecca Phillips
Okay, basically I got the same answer as Michelle (above) because Hello back to you! is printed with print r, Hello is printed only four times because for i in range (1,5) only tells Jes to print Hello four times (5 isn't printed), and value = value - 1 assigns 9 to the first Hello printed, 8 to the second Hello printed, 7 to the third Hello printed, and 6 to the fourth Hello printed. So then 6 is the final value printed for print value. Finally print r prints Hello back to you! again.
I hope this helps! I'm pretty sure its right.
Emily Lukach
Emily Lukach, why does yours print 6 if "print value" isn't inside the loop? Doesn't it have to be inside the loop in order to print "6"?
Rebecca Phillips
Well, even though "print value" isn't inside the loop "value = value-1" is inside the loop which decreases the value each time going through the loop. Finally, "print value" prints the last value after the loop is finished, which is 6.
Emily Lukach
Emily, okay, I understand why the last value should be 6; however, I have one more question. If the range was (2,8), how would it print? Would it print out 6 times? For instance, would it start on the second round, and sequence through to 7? I'm just trying to make sure I understand this. Thanks!
Rebecca Phillips
>>> print range(2,7)
[2, 3, 4, 5, 6]
BTW, did you know that you can also do this:
a = range(1,5)
for x in a:
print "Hello"
which looks like this when executed from the Command Area:
>>> a = range(1,5)
>>> for x in a:
... print "Hello"
...
Hello
Hello
Hello
Hello
Yes the range from (2,8) would theoretically print out 6 Hellos. However, the specific program we are given doesn't know that we are starting at 2 instead of 1. In other words it would still print out one less than 8 Hellos which would be 7. It would look like this.
>>> testme(8,51,"Hello back to you!")
Hello back to you!
Hello
Hello
Hello
Hello
Hello
Hello
Hello
3
Hello back to you!
Emily Lukach
I was wondering what would happen if the program started at 2 and the input was 8. I believe I already know what would happen (6 Hellos). Thanks for the clarification!
Rebecca Phillips
Link to this Page