2016-09-17 86 views
0

中供以后使用功能的Python店变量?函数中的变量可以在以后使用吗?

这是低于(未完)一个统计计算器:

#Statistics Calculator 
import random 
def main(mod): 
    print '' 
    if (mod == '1'): 
     print 'Mode 1 activated' 
     dat_entry = dat() 
    elif (mod == '2'): 
     print 'Mode 2 activated' 
     array = rndom(dat_entry) 
    elif (mod == '3'): 
     print 'Mode 3 activated' 
     array = user_input(dat_entry) 
    elif (mod == '4'): 
     disp(array) 
    elif (mod == '5'): 
     mean = mean(array) 
    elif (mod == '6'): 
     var = var(array) 
    elif (mod == '7'): 
     sd = sd(array, var) 
    elif (mod == '8'): 
     rang(array) 
    elif (mod == '9'): 
     median(array) 
    elif (mod == '10'): 
     mode(array) 
    elif (mod == '11'): 
     trim(array) 
    print '' 

def dat(): 
    dat = input('Please enter the number of data entries. ') 
    return dat 

def rndom(dat_entry): 
    print 'This mode allows the computer to generate the data entries.' 
    print 'It ranges from 1 to 100.' 
    cntr = 0 
    for cntr in range(cntr): 
     array[cntr] = random.randint(1,100) 
     print 'Generating Data Entry', cntr + 1 

def rndom(dat_entry): 
    print 'This mode allows you to enter the data.' 
    cntr = 0 
    for cntr in range(cntr): 
     array[cntr] = input('Please input the value of Data Entry ', 
          cntr + 1, ': ') 

run = 0 #Number of runs 
mod = '' #Mode 
cont = 'T' 
while (cont == 'T'): 
    print 'Statistics Calculator' 
    print 'This app can:' 
    print '1. Set the number of data entries.' 
    print '2. Randomly generate numbers from 1 to 100.' 
    print '3. Ask input from you, the user.' 
    print '4. Display array.' 
    print '5. Compute mean.' 
    print '6. Compute variance.' 
    print '7. Compute standard deviation.' 
    print '8. Compute range.' 
    print '9. Compute median.' 
    print '10. Compute mode.' 
    print '11. Compute trimmed mean.' 
    print '' 
    if (run == 0): 
     print 'You need to use Mode 1 first.' 
     mod = '1' 
    elif (run == 1): 
     while (mod != '2' or mod != '3'): 
      print 'Please enter Mode 2 or 3 only.' 
      mod = raw_input('Please enter the mode to use (2 or 3): ') 
      if (mod == '2' or mod == '3'): 
       break 
    elif (run > 1): 
     mod = raw_input('Please enter the mode to use (1-11): ') 
    # Error line 
    main(mod) 
    cont = raw_input("Please enter 'T' if and only if you want to continue" 
        " using this app. ") 
    run += 1 
    print '' 

这里这条线是输出(修整): 模式2激活

Traceback (most recent call last): 
    File "F:\Com SciActivities\Statistics.py", line 81, in <module> 
     main(mod) 
    File "F:\Com Sci Activities\Statistics.py", line 10, in main 
     array = rndom(dat_entry) 
    UnboundLocalError: local variable 'dat_entry' referenced before assignment 

请告诉我为什么.. 。

回答

0

的这部分代码是有问题的。 elif (mod == '2'): print 'Mode 2 activated' array = rndom(dat_entry)

Python不知道什么是“dat_entry”是rndom(dat_entry),因为它尚未以前在功能分配,这就是为什么它抛出一个错误。

我检查你的rndom(dat_entry)函数,但我看到的是“dat_entry”未在任何地方使用。所以这是我的建议。你可以用 def rndom(dat_entry): 替换为: def rndom(): 也许这样会修复那部分代码。

+0

我没有足够的声誉上的主要问题发表意见,我已经提交了答案,但我只注意到rndom(dat_entry)在代码中定义了两次。我建议删除其中一个定义。此外,我不认为rndom(dat_entry)函数对程序有什么实际影响,看看它的写法。 –

0

有与逻辑有问题。如果你直接进入模式2,这会导致这个错误,因为“dat_entry”将会是未定义的。

您已选择模式2,在这一点上不知道什么dat_entry是:

elif (mod == '2'): 
    print 'Mode 2 activated' 
    array = rndom(dat_entry) 

您应该声明dat_entry某处你的主回路或这里的某个地方,一旦用户选择了选项2:

 if (mod == '2' or mod == '3'): 
      break 
0

如果第一if条件不被满足,它不会执行与之相关联的代码的块。由于第一if条件不满足,则分配dat_entry = dat()将不会执行。

所以,你的情况,你可以做的做到这一点,如下所示:

elif (mod == '2'): 
    print 'Mode 2 activated' 
    array = rndom(dat()) 
elif (mod == '3'): 
    print 'Mode 3 activated' 
    array = user_input(dat()) 

请让我知道如果你需要任何其他说明。

相关问题