为什么python程序中有多个循环会出错?

为什么python程序中有多个循环会出错?,python,loops,Python,Loops,我正试图制作一个程序,计算出树上有多少苹果,一些落下,一些拿走,还有一些出现 不同的阶段是四棵不同的树,每棵树可以给其他树苹果。例如,树c将损失0.12(概率)个苹果,但树b将获得0.28(概率)个苹果 我的程序有错误,我只是不知道在哪里。我需要程序运行20次,但只运行1次。 from __future__ import division from pylab import* n=input("please enter the number of runs: ") a=50 b=40 c=60

我正试图制作一个程序,计算出树上有多少苹果,一些落下,一些拿走,还有一些出现

不同的阶段是四棵不同的树,每棵树可以给其他树苹果。例如,树c将损失0.12(概率)个苹果,但树b将获得0.28(概率)个苹果

我的程序有错误,我只是不知道在哪里。我需要程序运行20次,但只运行1次。

from __future__ import division
from pylab import*
n=input("please enter the number of runs: ")

a=50
b=40
c=60
d=80

#stage 1
i=0
while i<=n:
    a1=a-(a*0.24)
    b1=b-(b*0.29)-(b*0.28)+(d*0.09)
    c1=c-(c*0.12)+(b*0.28)
    d1=d-(d*0.09)+(d*0.24)+(b*0.29)+(c+0.12)
    i=i+1
    if a1<a:
        print "the population of apples in stage one after run ", n,
        print "has decreased to ", a1
    else:
        print "the population of apples in stage one after run ",n, 
        print " has increased to ", a1

#stage 2
    if b1<b:
        print "the population of apples in stage two after run ", n,
        print "has decreased to ", b1  
    else:
        print "the population of apples in stage two after run ", n, 
        print " has increased to ", b1
#stage 3
    if c1<c:
        print "the population of apples in stage three after run ", n,
        print "has decreased to ", c1
    else:
        print "the population of apples in stage three after run ",n, 
        print " has increased to ", c1
#stage 4
    if d1<d:
        print "the population of apples in stage four after run ", n,
        print "has decreased to", d1
    else:
        print "the population of apples in stage four after run ",n, 
        print " has increased to ", d1
        i=i+1
来自未来进口部的

从派拉布进口*
n=输入(“请输入运行次数:”)
a=50
b=40
c=60
d=80
#第一阶段
i=0

当i你需要从1开始i,然后在每次跑步后增加i,你使用的是n而不是i来跟踪每次跑步

from pylab import*
n=input("please enter the number of runs: ")

a=50
b=40
c=60
d=80

#stage 1
i=1
while i<=n:
    a1=a-(a*0.24)
    b1=b-(b*0.29)-(b*0.28)+(d*0.09)
    c1=c-(c*0.12)+(b*0.28)
    d1=d-(d*0.09)+(d*0.24)+(b*0.29)+(c+0.12)
    print i
    if a1<a:
        print "the population of apples in stage one after run ", i, # needs to be i not n
        print "has decreased to ", a1
    else:
        print "the population of apples in stage one after run ",i,
        print " has increased to ", a1

#stage 2
    if b1<b:
        print "the population of apples in stage two after run ", i,
        print "has decreased to ", b1
    else:
        print "the population of apples in stage two after run ", i,
        print " has increased to ", b1
#stage 3
    if c1<c:
        print "the population of apples in stage three after run ", i,
        print "has decreased to ", c1
    else:
        print "the population of apples in stage three after run ",i,
        print " has increased to ", c1
#stage 4
    if d1<d:
        print "the population of apples in stage four after run ", i,
        print "has decreased to", d1
    else:
        print "the population of apples in stage four after run ",i,
        print " has increased to ", d1
    i+=1
从pylab导入*
n=输入(“请输入运行次数:”)
a=50
b=40
c=60
d=80
#第一阶段
i=1

当i时,您没有在每个循环上打印
i
的值。您正在打印
n

from __future__ import division

n = input("please enter the number of runs: ")

a=50
b=40
c=60
d=80

def runLog(stage, run):
    print "the population of apples in stage {} after run {}".format(stage, run)

def appleLog(original, modified, stage, run):
    if modified < original:
        runLog(stage, run)
        print "has decreased to ", modified
    else:
        runLog(stage, run) 
        print " has increased to ", modified

for i in range(n+1):
    a1=a-(a*0.24)
    b1=b-(b*0.29)-(b*0.28)+(d*0.09)
    c1=c-(c*0.12)+(b*0.28)
    d1=d-(d*0.09)+(d*0.24)+(b*0.29)+(c+0.12)
    #stage 1
    appleLog(a, a1, stage=1, run=i)
    #stage 2
    appleLog(b, b1, stage=2, run=i)
    #stage 3
    appleLog(c, c1, stage=3, run=i)
    #stage 4
    appleLog(d, d1, stage=4, run=i)
我只能通过使用函数
runLog
appleLog
重构代码来找到错误。使用好的变量名和函数模块化对于避免错误和节省时间和精力都有很大帮助


另外,我使用了
for
而不是
while
,因为它更适合您的应用程序。使用
n+1
捕获
iYes Padraic是正确的,我同意Chris的观点。但我没有看到“n”被显式转换为“int”。如果不将“n”转换为“int”,它将在while循环中失败,并出现“TypeError:unorderable types:int()请修复代码的缩进。另外,为什么在一次迭代中增加i两次?while的位置有点奇怪。在我从pylab import*
中删除
后,它会运行44次。您应该将此程序分解为函数,并使用更具描述性的变量名。还应该只有一个
i=i+1
行,但我通过删除
pylab
import使其正常工作
n = input("please enter the number of runs: ")
n = int(n)