for loop
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Oct 22 18:47:55 EDT 2007
On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
> I'm having problems with my for loop here to calculate estimated
> population output to a table.
The code you have posted has inconsistent indentation. It won't run as
you supply it.
> Instead of knowing how much I want to
> loop it, the loop index is going to be whatever number of days the user
> enters.
Don't use input(), it doesn't do what you think it does.
Use int(raw_input()) instead.
> When I run my program, it asks the 3 questions above but then
> just dead stops at a prompt which leads me to believe there's something
> wrong with my loop.
Correcting the indentation in your code shows that it doesn't "just dead
stops at a prompt". So either you've posted the wrong code, or you aren't
telling us something. Is it possible that your program is raising an
exception telling you that something is wrong?
> I have the exact same setup in another program, but
> the loop index has a specific value.
Loop indexes always have a specific value. I think what you mean is that
it has a fixed, hard-coded value e.g. range(10) instead of range(x).
> I tried (0,days), (1,days) ect.
Ah, the time-honored technique of "debugging by making random changes".
range(days) is the correct thing to use, provided days is a positive
whole number greater than zero.
> and I don't think for loops need accumulators, I've tried it with one
> anyways and it still stops.
I thought you said the problem was that the loop didn't run at all? The
loop is supposed to stop.
> Any idea's?
Yes.
Your code has at least one bug in it. When you run that code, Python
prints an exception telling you what is wrong. That is useful
information, don't just ignore it.
--
Steven.
More information about the Python-list
mailing list