Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.


This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 1 Review Spring 2004: What gets printed

(Return to Sp2004 Midterm 1 Review)

Questions, answers, comments on answers, comments on questions?



I am confused on this one..... By reading the recipe, I got that it would print this: Hello back to you!
Hello
Hello
Hello
Hello
5
Hello back to you!

BUT, when I executed it in JES to see if I was right, it printed the #6 instead of 5....help...

Remember how the numbers in a range work: (1,5) would be 1,2,3,4–the last value would not be included. Try your math one REMOVED time with that idea and you should get the correct number. Student117

I am confused at what the value=10 has to do with the function... also the 6 in what the program prints out. Any guidance?

value = 10 means that the value of "value" the first time through the loop is 10-1, then 9-1, then 8-1, then 7-1. So, once the loop is finished the value is 6. So here's what I think the answer is:

Hello back to you!
Hello
Hello
Hello
Hello
6
Hello back to you!

Student1217

Remember that the value=value-1 works just like count=count+1 (from Breakout this week). Each time value=value-1 is executed, the value of value decreases by 1. Mark Guzdial


in what andrea posted, how do you know the loop stops at 7-1? i really don't understand this step at all.

Here's the code:
def testme(p,q,r):
  if q > 50:
    print r
  value = 10
  for i in range(1,p):
    print "Hello"
    value = value - 1
  print value
  print r

The code is called with testme(5,51,"Hello back to you!"). That means the loop is for i in range(1,5) (replacing the p with its input value of 5. Thus the loop goes (1,2,3,4). Each time through that loop, value gets decreased by 1. Does that make sense? Mark Guzdial


what does the i mean in "for i in range(1,p)"???
i is the index variable. It's the variable that will take on the values (given the input value of "5" for p) 1, 2, 3, and 4. Mark Guzdial

Should there be either a 5 or a 6 in the product?
What does it mean when we "print value"? Isn't the value = 10?
So, shouldn't the code be either:

(possibility 1)
Hello back to you!
Hello
Hello
Hello
Hello
10
Hello back to you!

(possibility 2)
Hello back to you!
Hello
Hello
Hello
Hello
Hello back to you!

Or, am I missing something?

Student1310
Lauren, the value of value CHANGES as the program executes. Each time value = value - 1 is executed, the value in value changes, becomes one less. So, when we execute print value at the end of the program, it prints the FINAL value of the variable value. Mark Guzdial

andrea's right

I understand, sort of, where Andrea is coming from..BUT..how do you know it stops at 6??? Student1113

Trace it, Kyla. Walk it through line-by-line. Count how many times we subtract one from value. Mark Guzdial

Call
testme(5,51,"Hello back to you")
if q=51>50:
print "Hello back to you"
value = 10
for i in range (1,2,3,4)
print "Hello"
value = value – 1
  1. value = 10-1=9 (1)
  2. value = 9-1=8 (2)
  3. value = 8-1=7 (3)
  4. value = 7-1=6 (4)
"Hello" (1)
"Hello" (2)
"Hello" (3)
"Hello" (4)
print 6
print “Hello back to you”


Hey! It stops at 6 because the range for the loop is (1,5) which means it goes through the loop 1,2,3,4. So it goes through 4 times. The explanation just above is really good! Good luck! Student1217

So, you're starting at the value of 10, then subtracting 1 each time until you almost tell the program to stop and tell you what number it's att (which would be 6)? Does that make sense, or I am way off? Student1192
Karin, I think you are on the right track. Your wording is a little off though. The program prints the number after the loop finishes. Student549

Is it the right answer to this question?
  1. from the 1st print:
hello back to you
  1. then from the for loop:
Hello
9
Hello back to you
Hello
8
Hello back to you
Hello
7
Hello back to you
Hello
6
Hello back to you
( Is the last print r part of the for loop or not?)
Maryam Doroudi


Mayam, look at the code again. The loop only contains one print statement. It only prints "hello" every time it loops. The "print value" and the "print r" at the end are not in the loop. Student549

What I have:
Hello back to you!
Hello
Hello
Hello
Hello
6
Hello back to you!
Student1189

I understand how REMOVED and others got that answer, but I have this question:
Shouldn't the print value command be INSIDE the for loop in order for the value to reduced each time through the loop?

Nope – print value simply displays a text representation of the contents of the variable value. It doesn't change value. The line value=value-1 is the one that actually changes value. Mark Guzdial



Link to this Page