• anonymous
hello world works , lists functions work but for loops freeze help
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
  • chestercat
I got my questions answered at in under 10 minutes. Go to now for free help!
  • anonymous
The most likely problem, in my experience, is that you're not putting the ':' at the end of the line. A for loop tells the computer to do something some number of times, and uses a specific variable to track the current number of times. So the statement for x in range (0,9): tells x to do something nine times. You need to indent everything you want the program to do in the loop on the next lines, so for instance for x in range(0,9): print x prints 0 1 2 3 4 5 6 7 8 There are some important things to note about that statement. First, the range function. range can take three arguments--there are three things that can go inside the parentheses. The first thing is the initial value of the variable, in this case x. This is optional and will default to 0 if it's not specified. So the statement for x in range(9): print x does exactly the same thing as for x in range(0,9): print x but if for some reason I didn't want x to start at 0, I could type for x in range(1,9): print x and the program would print 1 2 3 4 5 6 7 8 range can also take a 'step' argument; that is, you can specify how much you want x to increase each time the loop runs. The step argument has to be the third argument in the parentheses; that is, if you want to specify a step you cannot omit the beginning of the range. So the statement for x in range(0,9,2): print x prints 0 2 4 6 8 That is, x starts at 0, so it first prints 0. For each time after that, while x is less than 9, it increases by 2, because that's the step I specified in the argument. The last thing about the range function is that the idea of 'while x is less than 9' is important. Specifically, a range will never include the last integer you specify; in the case of for x in range(0,9): the loop runs for each integer less than 9 but does NOT run for 9. This is where it gets kind of confusing. Because x starts at 0, the loop actually does run 9 times, even though it stops at 8. This becomes important when you're trying to go through elements in a series. Finally, you'll notice that I didn't put any statement inside the loop modifying x. This is because the step argument (which Python interprets as 1 if you don't specify it) means that Python will go through every value of x (in the range) using the correct step. You CANNOT modify this with statements inside the loop. So for x in range(0,9): print x as we know, prints 0 through 8. The statements for x in range(0,9): print x x = x+ 2 ALSO prints the same thing: 0 1 2 3 4 5 6 7 8 You cannot modify the next value of x in the range from within the loop--at the beginning of each loop, x automatically gets assigned to the next integer in the range, regardless of what goes on inside the loop. This is one of the major differences between for and while loops; in a while loop, you have to make sure you are doing something inside the loop to change the variable, or else the loop will never terminate. As always, whitespace is important. The statement for x in range(0,9): print x will not run. You'll see the message 'IndentationError: expected an indented block' because the Python interpreter uses the position of the statements on each line to determine what happens inside the loop. So for x in range(0,9): print x works, but for x in range(0,9): print x does not work.

Looking for something else?

Not the answer you are looking for? Search for more explanations.