Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

for loop

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


d.lapasset" at bag

Dec 12, 2004, 9:18 AM

Post #1 of 19 (2445 views)
Permalink
for loop

Hello,
I have seen a lot of way to use the for statement, but I still don't know
how to do:

for i=morethanzero to n
or
for i= 5 to len(var)

of course I kno wthat I can use a while loop to achieve that but if this is
possible whith a for one, my preference goes to the for.

regards,
Dominique.


--
http://mail.python.org/mailman/listinfo/python-list


steven.bethard at gmail

Dec 12, 2004, 9:27 AM

Post #2 of 19 (2382 views)
Permalink
Re: for loop [In reply to]

houbahop wrote:
> for i=morethanzero to n

for i in range(morethanzero, n):
...

> for i= 5 to len(var)

for i in range(5, len(var)):
...

although range(len(var)) is usually not what you want, because you can
just do:

for item in itertools.islice(var, 5, None):
...

or if you really do need the index too:

for i, item in itertools.islice(enumerate(var), 5, None):
...

Steve
--
http://mail.python.org/mailman/listinfo/python-list


deetsNOSPAM at web

Dec 12, 2004, 10:04 AM

Post #3 of 19 (2385 views)
Permalink
Re: for loop [In reply to]

> for i in range(morethanzero, n):
> ...
>
>> for i= 5 to len(var)
>
> for i in range(5, len(var)):
> ...

Better use xrange - it doesn't create an actual list, but instead an
iterator enumerating the elements. That way more memory and cpu efficient.
--
Regards,

Diez B. Roggisch
--
http://mail.python.org/mailman/listinfo/python-list


steven.bethard at gmail

Dec 12, 2004, 10:47 AM

Post #4 of 19 (2384 views)
Permalink
Re: for loop [In reply to]

Diez B. Roggisch wrote:
>>
>>for i in range(5, len(var)):
>> ...
>
> Better use xrange - it doesn't create an actual list, but instead an
> iterator enumerating the elements. That way more memory and cpu efficient.

I could've sworn I read somewhere in the past that xrange was supposed
to be slower than range in some situation, but at least for some simple
cases in Python 2.4, it seems to be about as fast or faster:

> python -m timeit "for i in range(100): a = i*2"
10000 loops, best of 3: 21.8 usec per loop

> python -m timeit "for i in xrange(100): a = i*2"
10000 loops, best of 3: 19.4 usec per loop

> python -m timeit "for i in range(10000): a = i*2"
100 loops, best of 3: 2.18 msec per loop

> python -m timeit "for i in xrange(10000): a = i*2"
100 loops, best of 3: 2.29 msec per loop

> python -m timeit "for i in range(100000): a = i*2"
10 loops, best of 3: 25.5 msec per loop

> python -m timeit "for i in xrange(100000): a = i*2"
10 loops, best of 3: 20.7 msec per loop

I hardly ever use (x)range for anything (because you can almost always
use a for loop to loop over items instead), but I'll file this info away
mentally for the next time I do. Thanks!

Steve
--
http://mail.python.org/mailman/listinfo/python-list


loritsch at gmail

Dec 12, 2004, 11:23 AM

Post #5 of 19 (2385 views)
Permalink
Re: for loop [In reply to]

houbahop wrote:
> Hello,
> I have seen a lot of way to use the for statement, but I still don't
know
> how to do:
>
> for i=morethanzero to n
> or
> for i= 5 to len(var)
>
> of course I kno wthat I can use a while loop to achieve that but if
this is
> possible whith a for one, my preference goes to the for.
>
> regards,
> Dominique.

I think the key point that hasn't been made here is what a for
statement is really doing in python...

>From the online documentation:

"The for statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object"

Neither of the statements:
> for i=morethanzero to n
> for i= 5 to len(var)
create a sequence or iterable object, and that is why they don't work.

That is why previous posts in this thread have suggested using range(),
xrange(), etc. Because they create a sequence or iterable object.
When first using python, I recall that this distinction was not clear
to me, as I was used to a more traditional for statement (as in
C/C++):

for ( initialise ; test ; update )

Essentially, python's for statement is more like a foreach statement in
many other languages (Perl, C#, etc). These statements essentially
reduce the traditional form above to what many consider a more readable
form:

foreach item in collection:

In order to transform the tradition for statement into this more
readable form, each language requires that their collections being
iterated over satisfy a precondition defined by the language (in python
this precondition is that the collection is iterable).

While this restriction may seem a little strange to people who are used
to the more traditional for statement form, the result of this approach
is often a more readable and clear statement.
Regards,

Michael Loritsch

--
http://mail.python.org/mailman/listinfo/python-list


d.lapasset" at bag

Dec 12, 2004, 12:54 PM

Post #6 of 19 (2385 views)
Permalink
Re: for loop [In reply to]

thank you,
Whith python, It seems that all that I have learn in other languages is not
directly usable :) but looks like python is more efficient.
dominique.

<loritsch [at] gmail> a écrit dans le message de news:
1102879412.398726.191760 [at] c13g2000cwb
> houbahop wrote:
>> Hello,
>> I have seen a lot of way to use the for statement, but I still don't
> know
>> how to do:
>>
>> for i=morethanzero to n
>> or
>> for i= 5 to len(var)
>>
>> of course I kno wthat I can use a while loop to achieve that but if
> this is
>> possible whith a for one, my preference goes to the for.
>>
>> regards,
>> Dominique.
>
> I think the key point that hasn't been made here is what a for
> statement is really doing in python...
>
>>From the online documentation:
>
> "The for statement is used to iterate over the elements of a sequence
> (such as a string, tuple or list) or other iterable object"
>
> Neither of the statements:
>> for i=morethanzero to n
>> for i= 5 to len(var)
> create a sequence or iterable object, and that is why they don't work.
>
> That is why previous posts in this thread have suggested using range(),
> xrange(), etc. Because they create a sequence or iterable object.
> When first using python, I recall that this distinction was not clear
> to me, as I was used to a more traditional for statement (as in
> C/C++):
>
> for ( initialise ; test ; update )
>
> Essentially, python's for statement is more like a foreach statement in
> many other languages (Perl, C#, etc). These statements essentially
> reduce the traditional form above to what many consider a more readable
> form:
>
> foreach item in collection:
>
> In order to transform the tradition for statement into this more
> readable form, each language requires that their collections being
> iterated over satisfy a precondition defined by the language (in python
> this precondition is that the collection is iterable).
>
> While this restriction may seem a little strange to people who are used
> to the more traditional for statement form, the result of this approach
> is often a more readable and clear statement.
> Regards,
>
> Michael Loritsch
>


--
http://mail.python.org/mailman/listinfo/python-list


deetsNOSPAM at web

Dec 12, 2004, 3:48 PM

Post #7 of 19 (2383 views)
Permalink
Re: for loop [In reply to]

> I hardly ever use (x)range for anything (because you can almost always
> use a for loop to loop over items instead), but I'll file this info away
> mentally for the next time I do. Thanks!

Thats true for me too - and since enumerate was introduced, the number of
applications of xrange have decreased further.


--
Regards,

Diez B. Roggisch
--
http://mail.python.org/mailman/listinfo/python-list


binux.lists at gmail

Dec 12, 2004, 8:56 PM

Post #8 of 19 (2384 views)
Permalink
Re: for loop [In reply to]

If you are used to the C kind of for loops, avoid mistakes like this one:

>>> for i in range(1,6):
... print i
... i+=2
...
1
2
3
4
5

Obvious if you know you are iterating over a sequence.
--
http://mail.python.org/mailman/listinfo/python-list


bj_666 at gmx

Oct 22, 2007, 3:22 PM

Post #9 of 19 (2308 views)
Permalink
Re: for loop [In reply to]

On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:

> #Intro
> print "*********************************************"
> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
> print "*********************************************"
>
> print "This program will predict the size of a population of organisms."
> print
> print
> organisms=input("Please enter the starting number of organisms: ")
>
> increase=input("Please enter the average daily population increase
> as a percentage (20% = .20): ")
>
> days=input("Please enter the number of days that they will multiply: ")
>
> print " Day Population"
> print "----------------------------------------------------------"
>
>
> for p in range (days):
>
> population = organisms * population * increase
>
> print days,
>
> print "\t\t\t\t",population
>
> I'm having problems with my for loop here to calculate estimated
> population output to a table. Instead of knowing how much I want to
> loop it, the loop index is going to be whatever number of days the user
> enters. 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.

It should not run at all as it is indented inconsistently. If that
problem is corrected it will stop with a `NameError` because you try to
read `population` before anything was assigned to it.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Oct 22, 2007, 3:37 PM

Post #10 of 19 (2307 views)
Permalink
Re: for loop [In reply to]

On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
> > #Intro
> > print "*********************************************"
> > print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
> > print "*********************************************"
>
> > print "This program will predict the size of a population of organisms."
> > print
> > print
> > organisms=input("Please enter the starting number of organisms: ")
>
> > increase=input("Please enter the average daily population increase
> > as a percentage (20% = .20): ")
>
> > days=input("Please enter the number of days that they will multiply: ")
>
> > print " Day Population"
> > print "----------------------------------------------------------"
>
> > for p in range (days):
>
> > population = organisms * population * increase
>
> > print days,
>
> > print "\t\t\t\t",population
>
> > I'm having problems with my for loop here to calculate estimated
> > population output to a table. Instead of knowing how much I want to
> > loop it, the loop index is going to be whatever number of days the user
> > enters. 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.
>
> It should not run at all as it is indented inconsistently. If that
> problem is corrected it will stop with a `NameError` because you try to
> read `population` before anything was assigned to it.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Also, I would guess that you want to print p, not days.

--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Oct 22, 2007, 3:47 PM

Post #11 of 19 (2307 views)
Permalink
Re: for loop [In reply to]

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.
--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Oct 22, 2007, 4:05 PM

Post #12 of 19 (2309 views)
Permalink
Re: for loop [In reply to]

On Oct 22, 5:37 pm, "mensana...@aol.com" <mensana...@aol.com> wrote:
> On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
>
>
>
>
>
> > On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
> > > #Intro
> > > print "*********************************************"
> > > print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
> > > print "*********************************************"
>
> > > print "This program will predict the size of a population of organisms."
> > > print
> > > print
> > > organisms=input("Please enter the starting number of organisms: ")
>
> > > increase=input("Please enter the average daily population increase
> > > as a percentage (20% = .20): ")
>
> > > days=input("Please enter the number of days that they will multiply: ")
>
> > > print " Day Population"
> > > print "----------------------------------------------------------"
>
> > > for p in range (days):
>
> > > population = organisms * population * increase
>
> > > print days,
>
> > > print "\t\t\t\t",population
>
> > > I'm having problems with my for loop here to calculate estimated
> > > population output to a table. Instead of knowing how much I want to
> > > loop it, the loop index is going to be whatever number of days the user
> > > enters. 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.
>
> > It should not run at all as it is indented inconsistently. If that
> > problem is corrected it will stop with a `NameError` because you try to
> > read `population` before anything was assigned to it.
>
> > Ciao,
> > Marc 'BlackJack' Rintsch
>
> Also, I would guess that you want to print p, not days

Oh, and your calculation is incorrect. You don't multiply by
organisms in every loop iteration, organisms is the initial
value of population, so you can solve the "Name" error by doing
population = organisms before the for..loop.

And since you're asking for an increase, you don't multiply by the
percent (as that would decrease the population), but instead by
1+increase.

Also, does day==0 represent the first day of increase or the
initial value? One would normally expect day==0 to be the initial
value, but as written, day==0 is the first day of increase. I
would use xrange(1,days+1) instead.

Lastly, you can't have a fraction of an organism, right? You might
want to print your floating point population rounded to an integer.

population = organisms
for p in xrange(1,days+1):
population = population * (1 + increase)
print p,
print "\t\t\t\t%0.0f" % (population)

## *********************************************
## WELCOME TO THE POPULATION GROWTH CALCULATOR
## *********************************************
## This program will predict the size of a population of organisms.
##
##
## Please enter the starting number of organisms: 100
## Please enter the average daily population increase as a percentage
(20% = .20): 0.25
## Please enter the number of days that they will multiply: 8
## Day Population
## ----------------------------------------------------------
## 1 125
## 2 156
## 3 195
## 4 244
## 5 305
## 6 381
## 7 477
## 8 596


--
http://mail.python.org/mailman/listinfo/python-list


trekker182 at comcast

Oct 22, 2007, 7:12 PM

Post #13 of 19 (2293 views)
Permalink
Re: for loop [In reply to]

Thanks, everyone! Using everyone's suggestions and points, the program
is working great now. Here's the updated code.

:)

import math

def main():
#Declare and initialize variables
#starting number of organisms
organisms = 0
#average daily population increase as %
increase = 0.0
#number of days they will multiply
days = 0
#population prediction
population = 0.0

#Intro
print "*********************************************"
print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
print "*********************************************"

print "This program will predict the size of a population of organisms."
print
print
while organisms <=1:
organisms=input("Please enter the starting number of organisms: ")
if organisms <=1:
print "Error. Population must be at least two."

while increase <=0:
increase=input("Please enter the average daily population
increase as a percentage (20% = .20): ")
if increase <=0:
print "The percent of increase must be positive."

while days <=0:
days=input("Please enter the number of days that they will
multiply: ")
if days <=0:
print "The number of days must be positive."

print " Day Population"
print "----------------------------------------------------------"
population = organisms


for p in range (1,days+1):
if( p > 1 ):
population = population + ( population * increase )


print "\t",p,

mensanator [at] aol wrote:
> On Oct 22, 5:37 pm, "mensana...@aol.com" <mensana...@aol.com> wrote:
>
>> On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
>>
>>
>>
>>
>>
>>
>>> On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
>>>
>>>> #Intro
>>>> print "*********************************************"
>>>> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
>>>> print "*********************************************"
>>>>
>>>> print "This program will predict the size of a population of organisms."
>>>> print
>>>> print
>>>> organisms=input("Please enter the starting number of organisms: ")
>>>>
>>>> increase=input("Please enter the average daily population increase
>>>> as a percentage (20% = .20): ")
>>>>
>>>> days=input("Please enter the number of days that they will multiply: ")
>>>>
>>>> print " Day Population"
>>>> print "----------------------------------------------------------"
>>>>
>>>> for p in range (days):
>>>>
>>>> population = organisms * population * increase
>>>>
>>>> print days,
>>>>
>>>> print "\t\t\t\t",population
>>>>
>>>> I'm having problems with my for loop here to calculate estimated
>>>> population output to a table. Instead of knowing how much I want to
>>>> loop it, the loop index is going to be whatever number of days the user
>>>> enters. 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.
>>>>
>>> It should not run at all as it is indented inconsistently. If that
>>> problem is corrected it will stop with a `NameError` because you try to
>>> read `population` before anything was assigned to it.
>>>
>>> Ciao,
>>> Marc 'BlackJack' Rintsch
>>>
>> Also, I would guess that you want to print p, not days
>>
>
> Oh, and your calculation is incorrect. You don't multiply by
> organisms in every loop iteration, organisms is the initial
> value of population, so you can solve the "Name" error by doing
> population = organisms before the for..loop.
>
> And since you're asking for an increase, you don't multiply by the
> percent (as that would decrease the population), but instead by
> 1+increase.
>
> Also, does day==0 represent the first day of increase or the
> initial value? One would normally expect day==0 to be the initial
> value, but as written, day==0 is the first day of increase. I
> would use xrange(1,days+1) instead.
>
> Lastly, you can't have a fraction of an organism, right? You might
> want to print your floating point population rounded to an integer.
>
> population = organisms
> for p in xrange(1,days+1):
> population = population * (1 + increase)
> print p,
> print "\t\t\t\t%0.0f" % (population)
>
> ## *********************************************
> ## WELCOME TO THE POPULATION GROWTH CALCULATOR
> ## *********************************************
> ## This program will predict the size of a population of organisms.
> ##
> ##
> ## Please enter the starting number of organisms: 100
> ## Please enter the average daily population increase as a percentage
> (20% = .20): 0.25
> ## Please enter the number of days that they will multiply: 8
> ## Day Population
> ## ----------------------------------------------------------
> ## 1 125
> ## 2 156
> ## 3 195
> ## 4 244
> ## 5 305
> ## 6 381
> ## 7 477
> ## 8 596
>
>
>

--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Oct 22, 2007, 9:38 PM

Post #14 of 19 (2291 views)
Permalink
Re: for loop [In reply to]

On Oct 22, 9:12?pm, Shawn Minisall <trekker...@comcast.net> wrote:
> Thanks, everyone! Using everyone's suggestions and points, the program
> is working great now.

Actually, it's not. I assume not printing the population
was a copy error.

But, by adding the "if (p>1):", you have now made
day 1 the initial population, so if you ask for
8 days of multiplication, you actually only get 7.

Although your code is working, it's not giving you
the correct answer. Most of the time in these kinds
of problems, days means elapsed days. That means for
100 organisms @ 25% growth/day, there will be 125
after 1 elapsed day. But that "if (p>1):" means you
show 100 at day 1, which is wrong. You have 100 after
0 elapsed days (or day 0).


> Here's the updated code.
>
> :)
>
> import math
>
> def main():
> #Declare and initialize variables
> #starting number of organisms
> organisms = 0
> #average daily population increase as %
> increase = 0.0
> #number of days they will multiply
> days = 0
> #population prediction
> population = 0.0
>
> #Intro
> print "*********************************************"
> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
> print "*********************************************"
>
> print "This program will predict the size of a population of organisms."
> print
> print
> while organisms <=1:
> organisms=input("Please enter the starting number of organisms: ")
> if organisms <=1:
> print "Error. Population must be at least two."
>
> while increase <=0:
> increase=input("Please enter the average daily population
> increase as a percentage (20% = .20): ")
> if increase <=0:
> print "The percent of increase must be positive."
>
> while days <=0:
> days=input("Please enter the number of days that they will
> multiply: ")
> if days <=0:
> print "The number of days must be positive."
>
> print " Day Population"
> print "----------------------------------------------------------"
> population = organisms
>
> for p in range (1,days+1):
> if( p > 1 ):
> population = population + ( population * increase )
>
> print "\t",p,
>
>
>
> mensana...@aol.com wrote:
> > On Oct 22, 5:37 pm, "mensana...@aol.com" <mensana...@aol.com> wrote:
>
> >> On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
>
> >>> On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
>
> >>>> #Intro
> >>>> print "*********************************************"
> >>>> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
> >>>> print "*********************************************"
>
> >>>> print "This program will predict the size of a population of organisms."
> >>>> print
> >>>> print
> >>>> organisms=input("Please enter the starting number of organisms: ")
>
> >>>> increase=input("Please enter the average daily population increase
> >>>> as a percentage (20% = .20): ")
>
> >>>> days=input("Please enter the number of days that they will multiply: ")
>
> >>>> print " Day Population"
> >>>> print "----------------------------------------------------------"
>
> >>>> for p in range (days):
>
> >>>> population = organisms * population * increase
>
> >>>> print days,
>
> >>>> print "\t\t\t\t",population
>
> >>>> I'm having problems with my for loop here to calculate estimated
> >>>> population output to a table. Instead of knowing how much I want to
> >>>> loop it, the loop index is going to be whatever number of days the user
> >>>> enters. 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.
>
> >>> It should not run at all as it is indented inconsistently. If that
> >>> problem is corrected it will stop with a `NameError` because you try to
> >>> read `population` before anything was assigned to it.
>
> >>> Ciao,
> >>> Marc 'BlackJack' Rintsch
>
> >> Also, I would guess that you want to print p, not days
>
> > Oh, and your calculation is incorrect. You don't multiply by
> > organisms in every loop iteration, organisms is the initial
> > value of population, so you can solve the "Name" error by doing
> > population = organisms before the for..loop.
>
> > And since you're asking for an increase, you don't multiply by the
> > percent (as that would decrease the population), but instead by
> > 1+increase.
>
> > Also, does day==0 represent the first day of increase or the
> > initial value? One would normally expect day==0 to be the initial
> > value, but as written, day==0 is the first day of increase. I
> > would use xrange(1,days+1) instead.
>
> > Lastly, you can't have a fraction of an organism, right? You might
> > want to print your floating point population rounded to an integer.
>
> > population = organisms
> > for p in xrange(1,days+1):
> > population = population * (1 + increase)
> > print p,
> > print "\t\t\t\t%0.0f" % (population)
>
> > ## *********************************************
> > ## WELCOME TO THE POPULATION GROWTH CALCULATOR
> > ## *********************************************
> > ## This program will predict the size of a population of organisms.
> > ##
> > ##
> > ## Please enter the starting number of organisms: 100
> > ## Please enter the average daily population increase as a percentage
> > (20% = .20): 0.25
> > ## Please enter the number of days that they will multiply: 8
> > ## Day Population
> > ## ----------------------------------------------------------
> > ## 1 125
> > ## 2 156
> > ## 3 195
> > ## 4 244
> > ## 5 305
> > ## 6 381
> > ## 7 477
> > ## 8 596- Hide quoted text -
>
> - Show quoted text -


--
http://mail.python.org/mailman/listinfo/python-list


trekker182 at comcast

Oct 24, 2007, 3:30 PM

Post #15 of 19 (2291 views)
Permalink
Re: for loop [In reply to]

I agree, but if I want to get a A on the program, thats how my professor
wants the output.

:)

mensanator [at] aol wrote:
> On Oct 22, 9:12?pm, Shawn Minisall <trekker...@comcast.net> wrote:
>
>> Thanks, everyone! Using everyone's suggestions and points, the program
>> is working great now.
>>
>
> Actually, it's not. I assume not printing the population
> was a copy error.
>
> But, by adding the "if (p>1):", you have now made
> day 1 the initial population, so if you ask for
> 8 days of multiplication, you actually only get 7.
>
> Although your code is working, it's not giving you
> the correct answer. Most of the time in these kinds
> of problems, days means elapsed days. That means for
> 100 organisms @ 25% growth/day, there will be 125
> after 1 elapsed day. But that "if (p>1):" means you
> show 100 at day 1, which is wrong. You have 100 after
> 0 elapsed days (or day 0).
>
>
>
>> Here's the updated code.
>>
>> :)
>>
>> import math
>>
>> def main():
>> #Declare and initialize variables
>> #starting number of organisms
>> organisms = 0
>> #average daily population increase as %
>> increase = 0.0
>> #number of days they will multiply
>> days = 0
>> #population prediction
>> population = 0.0
>>
>> #Intro
>> print "*********************************************"
>> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
>> print "*********************************************"
>>
>> print "This program will predict the size of a population of organisms."
>> print
>> print
>> while organisms <=1:
>> organisms=input("Please enter the starting number of organisms: ")
>> if organisms <=1:
>> print "Error. Population must be at least two."
>>
>> while increase <=0:
>> increase=input("Please enter the average daily population
>> increase as a percentage (20% = .20): ")
>> if increase <=0:
>> print "The percent of increase must be positive."
>>
>> while days <=0:
>> days=input("Please enter the number of days that they will
>> multiply: ")
>> if days <=0:
>> print "The number of days must be positive."
>>
>> print " Day Population"
>> print "----------------------------------------------------------"
>> population = organisms
>>
>> for p in range (1,days+1):
>> if( p > 1 ):
>> population = population + ( population * increase )
>>
>> print "\t",p,
>>
>>
>>
>> mensana...@aol.com wrote:
>>
>>> On Oct 22, 5:37 pm, "mensana...@aol.com" <mensana...@aol.com> wrote:
>>>
>>>> On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
>>>>
>>>>> On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
>>>>>
>>>>>> #Intro
>>>>>> print "*********************************************"
>>>>>> print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
>>>>>> print "*********************************************"
>>>>>>
>>>>>> print "This program will predict the size of a population of organisms."
>>>>>> print
>>>>>> print
>>>>>> organisms=input("Please enter the starting number of organisms: ")
>>>>>>
>>>>>> increase=input("Please enter the average daily population increase
>>>>>> as a percentage (20% = .20): ")
>>>>>>
>>>>>> days=input("Please enter the number of days that they will multiply: ")
>>>>>>
>>>>>> print " Day Population"
>>>>>> print "----------------------------------------------------------"
>>>>>>
>>>>>> for p in range (days):
>>>>>>
>>>>>> population = organisms * population * increase
>>>>>>
>>>>>> print days,
>>>>>>
>>>>>> print "\t\t\t\t",population
>>>>>>
>>>>>> I'm having problems with my for loop here to calculate estimated
>>>>>> population output to a table. Instead of knowing how much I want to
>>>>>> loop it, the loop index is going to be whatever number of days the user
>>>>>> enters. 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.
>>>>>>
>>>>> It should not run at all as it is indented inconsistently. If that
>>>>> problem is corrected it will stop with a `NameError` because you try to
>>>>> read `population` before anything was assigned to it.
>>>>>
>>>>> Ciao,
>>>>> Marc 'BlackJack' Rintsch
>>>>>
>>>> Also, I would guess that you want to print p, not days
>>>>
>>> Oh, and your calculation is incorrect. You don't multiply by
>>> organisms in every loop iteration, organisms is the initial
>>> value of population, so you can solve the "Name" error by doing
>>> population = organisms before the for..loop.
>>>
>>> And since you're asking for an increase, you don't multiply by the
>>> percent (as that would decrease the population), but instead by
>>> 1+increase.
>>>
>>> Also, does day==0 represent the first day of increase or the
>>> initial value? One would normally expect day==0 to be the initial
>>> value, but as written, day==0 is the first day of increase. I
>>> would use xrange(1,days+1) instead.
>>>
>>> Lastly, you can't have a fraction of an organism, right? You might
>>> want to print your floating point population rounded to an integer.
>>>
>>> population = organisms
>>> for p in xrange(1,days+1):
>>> population = population * (1 + increase)
>>> print p,
>>> print "\t\t\t\t%0.0f" % (population)
>>>
>>> ## *********************************************
>>> ## WELCOME TO THE POPULATION GROWTH CALCULATOR
>>> ## *********************************************
>>> ## This program will predict the size of a population of organisms.
>>> ##
>>> ##
>>> ## Please enter the starting number of organisms: 100
>>> ## Please enter the average daily population increase as a percentage
>>> (20% = .20): 0.25
>>> ## Please enter the number of days that they will multiply: 8
>>> ## Day Population
>>> ## ----------------------------------------------------------
>>> ## 1 125
>>> ## 2 156
>>> ## 3 195
>>> ## 4 244
>>> ## 5 305
>>> ## 6 381
>>> ## 7 477
>>> ## 8 596- Hide quoted text -
>>>
>> - Show quoted text -
>>
>
>
>

--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 30, 2012, 3:49 AM

Post #16 of 19 (2175 views)
Permalink
Re: For loop [In reply to]

On Mon, Apr 30, 2012 at 8:41 PM, viral shah <shahviral.it [at] gmail> wrote:
> for i in range (5):
>   for j in range (i):
>     print i
>   print " "

The print command (you're clearly using Python 2 here - it's slightly
different in Python 3) by default prints a whole line - that is, it
finishes with a newline or '\n'. You'll find information on how to
suppress that here:

http://docs.python.org/reference/simple_stmts.html#the-print-statement

Hope that helps!

Chris Angelico
--
http://mail.python.org/mailman/listinfo/python-list


daniel at kingdread

Apr 30, 2012, 4:52 AM

Post #17 of 19 (2160 views)
Permalink
Re: For loop [In reply to]

You could also try http://docs.python.org/library/stdtypes.html#str.join
like this:
for i in range(5):
print "".join(str(i) for j in range(i))
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Apr 30, 2012, 11:55 AM

Post #18 of 19 (2160 views)
Permalink
Re: For loop [In reply to]

On 4/30/2012 6:41 AM, viral shah wrote:
> Hi
>
> I want to make a pattern like this
>
> *1
> 22
> 333
> 4444
> 55555

Python 3:
>>> for i in range(1,6): print(i*str(i))

1
22
333
4444
55555

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


jabba.laci at gmail

May 1, 2012, 7:23 AM

Post #19 of 19 (2161 views)
Permalink
Re: For loop [In reply to]

Hi,

Try this:

import sys

for i in range (1, 5+1):
for j in range (i):
sys.stdout.write(str(i))
print

"print" adds a newline character
print "hi", notice the comma, it won't add newline, however
it adds a space
With sys.stdout.write you can print the way you want, it won't add any
extra stuff (newline or space).

Best,

Laszlo


On Mon, Apr 30, 2012 at 13:52, Daniel <daniel [at] kingdread> wrote:
> You could also try http://docs.python.org/library/stdtypes.html#str.join
> like this:
> for i in range(5):
>    print "".join(str(i) for j in range(i))
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.