我有这个python程序,它出现的输出只是让我发疯

时间:2014-05-19 04:36:37

标签: python-3.x

print "This mode is called The Rat Population Change Mode(The Rat Maze Mode). The purpose of this mode is to determining the rat population change in each stage.By putting in the number of the steps you want to run this program, you can find out how the population has changed in each stage and a gragh wich indicates the change in visual."
n = input("Tell me the number of steps you want to run this progrem?:")


#Creat array and function for stage 1

Stage1 = zeros([n]) #Creat an array of value for stage 1

Stage1[0] = 50 #Creat the initial value of the stage 1 Array

Step1 = zeros([n]) #Creat Array of the population step in stage 1

def population1 (i):

    return Stage1[i - 1] + Stage4[i - 1] * 0.20 + Stage2[i - 1] * 0.20 - Stage1[i - 1] * 0.22

#Creat array and function for stage 2

Stage2 = zeros([n]) #Creat an array of value for stage 2

Stage2[0] = 40 #Creat the initial value of the stage 1 Array

Step2 = zeros([n]) #Creat Array of the population step in stage 2

def population2 (i):

    return Stage2[i - 1] + Stage3[i - 1] * 0.13

#Creat array and function for stage 3

Stage3 = zeros([n]) #Creat an array of value for stage 3

Stage3[0] = 50 #Creat the initial value of the stage 1 Array

Step3 = zeros([n]) #Creat Array of the population step in stage 3

def population3 (i):

    return Stage3[i - 1] - (Stage3[i - 1] * 0.20 + Stage3[i - 1] * 0.13 + Stage3[i - 1] * 0.16)

#Creat array and function for stage 4

Stage4 = zeros([n]) #Creat an array of value for stage 4

Stage4[0] = 10 #Creat the initial value of the stage 1 Array

Step4 = zeros([n]) #Creat Array of the population step in stage 4

def population4 (i):

    return Stage4[i - 1] + Stage1[i - 1] * 0.22 + Stage3[i - 1] * 0.16 - Stage4[i - 1] * 0.20 




#Creat the loop of population change and condition statement of each stage


i = 1

while i < n :

    Stage1[i] = population1 (i)
    if Stage1[i] < Stage1[i-1]  : #creat if statement for Stage 1
        PopulationChange1 = "decreased"
    elif Stage1[i] > Stage1[i-1] :
        PopulationChange1 = "increased"
    else :
        PopulationChange1 = "stayed the same"

    Stage2[i] = population2 (i)
    if Stage2[i] < Stage2[i-1]  : #creat if statement for Stage 2
        PopulationChange2 = "decreased"
    elif Stage2[i] > Stage2[i-1] :
        PopulationChange2 = "increased"
    else :
        PopulationChange2 = "stayed the same"

    Stage3[i] = population3 (i)
    if Stage3[i] < Stage3[i-1]  : #creat if statement for Stage 3
        PopulationChange3 = "decreased"
    elif Stage3[i] > Stage3[i-1] :
        PopulationChange3 = "increased"
    else :
        PopulationChange3 = "stayed the same"

    Stage4[i] = population4 (i)
    if Stage4[i] < Stage4[i-1]  : #creat if statement for Stage 4
        PopulationChange4 = "decreased"
    elif Stage4[i] > Stage4[i-1] :

        PopulationChange4 = "increased"
    else :
        PopulationChange4 = "stayed the same"

    Step1[i] = i #Creat statement for X axis in Stage 1
    Step2[i] = i #Creat statement for X axis in Stage 1
    Step3[i] = i #Creat statement for X axis in Stage 1
    Step4[i] = i #Creat statement for X axis in Stage 1

    i = i + 1


#print the value output and conditional statement for each stage

print " Stage 1 had an Initial Population of 50. After time step", n,", the population is", population1 (i)
print " The rat population after step",n,"in Stage 1 has",PopulationChange1,"from the initial rat population",Stage1[i-1]
print ""
print " Stage 2 had an Initial Population of 40. After time step", n,", the population is", population2 (i)
print " The rat population after step",n,"in Stage 2 has",PopulationChange2,"from the initial rat population",Stage2[i-1]
print ""
print " Stage 3 had an Initial Population of 50. After time step", n,", the population is", population3 (i)
print " The rat population after step",n,"in Stage 3 has",PopulationChange3,"from the initial rat population",Stage3[i-1]
print ""
print " Stage 4 had an Initial Population of 10. After time step", n,", the population is", population4 (i)
print " The rat population after step",n,"in Stage 4 has",PopulationChange4,"from the initial rat population",Stage4[i-1]
print ""

#plot the graph
plot(Step1, Stage1,linewidth=5, label='Stage 1')#plot(x,y,linewidth,label)
plot(Step2, Stage2,linewidth=5, label='Stage 2')
plot(Step3, Stage3,linewidth=5, label='Stage 3')
plot(Step4, Stage4,linewidth=5, label='Stage 4')

title ("The Rat Population Change Mode(The Rat Maze Mode)")
xlabel ("The Number of Steps")
ylabel ("The Change in The Population of Rat")

grid(True)
legend()

show() 

我遇到的问题是:

  1. 如果我输入1,我得到了这个输出:Traceback(最近一次调用最后一次):

    文件“C:/Users/Kaiyiming/Desktop/Actual.py”,第110行,在     打印“步骤后的大鼠群体”,n,“在第1阶段有”,PopulationChange1,“来自最初的大鼠群体”,第1阶段[i-1] NameError:名称'PopulationChange1'未定义

  2. 如果我输入2,我得到了这个输出:

    第1阶段的初始人口为50.在时间步骤2之后,人口为52.92 在阶段1中的步骤2之后的大鼠群体从最初的大鼠群体中减少了49.0

  3. 它应该增加但是它在这里减少了。这只发生在步骤1的第1阶段。我无法找出问题所在。

    Plz帮帮我

1 个答案:

答案 0 :(得分:1)

您的问题是如果i < n,您可以PopulationChange1。因此,当您将输入设为1时,它只会检查1 < 1False)是否为PopulationChange1,因此永远不会引用NameError,而是PopulationChange1 = PopulationChange2 = PopulationChange3 = PopulationChange4 = ""

要解决此问题,请将print "This mode is called The Rat Population Change Mode(The Rat Maze Mode). The purpose of this mode is to determining the rat population change in each stage.By putting in the number of the steps you want to run this program, you can find out how the population has changed in each stage and a gragh wich indicates the change in visual." n = input("Tell me the number of steps you want to run this progrem?:") #Add variables here to prevent error PopulationChange1 = PopulationChange2 = PopulationChange3 = PopulationChange4 = "" #Creat array and function for stage 1 Stage1 = zeros([n]) #Creat an array of value for stage 1 Stage1[0] = 50 #Creat the initial value of the stage 1 Array Step1 = zeros([n]) #Creat Array of the population step in stage 1 def population1 (i): return Stage1[i - 1] + Stage4[i - 1] * 0.20 + Stage2[i - 1] * 0.20 - Stage1[i - 1] * 0.22 #Creat array and function for stage 2 Stage2 = zeros([n]) #Creat an array of value for stage 2 Stage2[0] = 40 #Creat the initial value of the stage 1 Array Step2 = zeros([n]) #Creat Array of the population step in stage 2 def population2 (i): return Stage2[i - 1] + Stage3[i - 1] * 0.13 #Creat array and function for stage 3 Stage3 = zeros([n]) #Creat an array of value for stage 3 Stage3[0] = 50 #Creat the initial value of the stage 1 Array Step3 = zeros([n]) #Creat Array of the population step in stage 3 def population3 (i): return Stage3[i - 1] - (Stage3[i - 1] * 0.20 + Stage3[i - 1] * 0.13 + Stage3[i - 1] * 0.16) #Creat array and function for stage 4 Stage4 = zeros([n]) #Creat an array of value for stage 4 Stage4[0] = 10 #Creat the initial value of the stage 1 Array Step4 = zeros([n]) #Creat Array of the population step in stage 4 def population4 (i): return Stage4[i - 1] + Stage1[i - 1] * 0.22 + Stage3[i - 1] * 0.16 - Stage4[i - 1] * 0.20 #Creat the loop of population change and condition statement of each stage i = 1 while i < n : Stage1[i] = population1 (i) if Stage1[i] < Stage1[i-1] : #creat if statement for Stage 1 PopulationChange1 = "decreased" elif Stage1[i] > Stage1[i-1] : PopulationChange1 = "increased" else : PopulationChange1 = "stayed the same" Stage2[i] = population2 (i) if Stage2[i] < Stage2[i-1] : #creat if statement for Stage 2 PopulationChange2 = "decreased" elif Stage2[i] > Stage2[i-1] : PopulationChange2 = "increased" else : PopulationChange2 = "stayed the same" Stage3[i] = population3 (i) if Stage3[i] < Stage3[i-1] : #creat if statement for Stage 3 PopulationChange3 = "decreased" elif Stage3[i] > Stage3[i-1] : PopulationChange3 = "increased" else : PopulationChange3 = "stayed the same" Stage4[i] = population4 (i) if Stage4[i] < Stage4[i-1] : #creat if statement for Stage 4 PopulationChange4 = "decreased" elif Stage4[i] > Stage4[i-1] : PopulationChange4 = "increased" else : PopulationChange4 = "stayed the same" Step1[i] = i #Creat statement for X axis in Stage 1 Step2[i] = i #Creat statement for X axis in Stage 1 Step3[i] = i #Creat statement for X axis in Stage 1 Step4[i] = i #Creat statement for X axis in Stage 1 i = i + 1 #print the value output and conditional statement for each stage print " Stage 1 had an Initial Population of 50. After time step", n,", the population is", population1 (i) print " The rat population after step",n,"in Stage 1 has",PopulationChange1,"from the initial rat population",Stage1[i-1] print "" print " Stage 2 had an Initial Population of 40. After time step", n,", the population is", population2 (i) print " The rat population after step",n,"in Stage 2 has",PopulationChange2,"from the initial rat population",Stage2[i-1] print "" print " Stage 3 had an Initial Population of 50. After time step", n,", the population is", population3 (i) print " The rat population after step",n,"in Stage 3 has",PopulationChange3,"from the initial rat population",Stage3[i-1] print "" print " Stage 4 had an Initial Population of 10. After time step", n,", the population is", population4 (i) print " The rat population after step",n,"in Stage 4 has",PopulationChange4,"from the initial rat population",Stage4[i-1] print "" #plot the graph plot(Step1, Stage1,linewidth=5, label='Stage 1')#plot(x,y,linewidth,label) plot(Step2, Stage2,linewidth=5, label='Stage 2') plot(Step3, Stage3,linewidth=5, label='Stage 3') plot(Step4, Stage4,linewidth=5, label='Stage 4') title ("The Rat Population Change Mode(The Rat Maze Mode)") xlabel ("The Number of Steps") ylabel ("The Change in The Population of Rat") grid(True) legend() show() 放在代码的开头。

以下是您编辑的代码:

{{1}}