A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

hello world works , lists functions work but for loops freeze help

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.