Average calculation Program *need help*

Michael Torrie torriem at gmail.com
Fri May 13 01:29:47 EDT 2016


On 05/12/2016 11:03 PM, Jake Kobs wrote:
> Im not sure how to move it inside the for loop. I've been working on
> this small problem for like 4 hours lol.

I'm sorry it's so frustrating.  Sounds like you haven't got down some of
the most basic fundamentals yet.  In Python, things that should happen
one after another are placed at the same indent level on the screen.

For example:

if something:
    do_this()
    then_this()
    then_that()

Everything indented in from the start of the if statement is in a block
and only happens if the if statement condition is true.  If there're
statements at the same level as the if, then they happen *after* the if
and it's block.  In other words, indentation is what tells Python where
you want things to run.  If you want to move the while loop inside the
for loop, you have to adjust it's indentation accordingly (and the
indentation in it's own block).

This is a big gotcha for people unfamiliar with programming in Python,
and unfamiliar with programming in general.  In other languages, indents
don't have to be a certain way, as long as you have the write statements
to close off the loop.  However, Python's method forces you to think
like a programmer and to lay things out on the screen in a logical
fashion, like a writer's outline.

If you're still stuck, you will probably want to sit down with your
teacher and have him or her go over this with you.  This is important
basic stuff you need to have clear in your mind to program computers.



More information about the Python-list mailing list