![]() ![]() |
| |||||||||
| This page removed for FERPA compliance | ||||||||||
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
| 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 |
| 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 |
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
| 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 |
| 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 |
| Trace it, Kyla. Walk it through line-by-line. Count how many times we subtract one from value. Mark Guzdial |
| 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 |
| 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 |
| 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 |