user4502777
user4502777

Reputation:

Can variables in a function for later use?

Can Python store variables in a function for later use?

This is a stat calculator below (unfinished):

#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 ''

This line here is the output (trimmed): Mode 2 activated

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

Please tell me the reason why...

Upvotes: 1

Views: 50

Answers (3)

Jeril
Jeril

Reputation: 8551

If the first if condition is not satisfied, it wont execute the block of codes associated with it. Since the first if condition is not satisfied, the assignment dat_entry = dat() will not execute.

So, in your case you can do the do it as follows:

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

Kindly let me know if you need any other clarifications.

Upvotes: 0

B B
B B

Reputation: 1196

This part of the code is problematic. elif (mod == '2'): print 'Mode 2 activated' array = rndom(dat_entry)

Python does not know what 'dat_entry' is in rndom(dat_entry) because it has not yet been assigned previously in the function, that's why it's throwing an error.

I checked your rndom(dat_entry) function, but I see that 'dat_entry' is not used anywhere within it. So this is what I suggest. You can replace def rndom(dat_entry): with: def rndom(): Maybe that would fix that part of code.

Upvotes: 0

d1ll1nger
d1ll1nger

Reputation: 1701

There's a problem with the logic. If you go straight to mode 2 this is what will cause this error because "dat_entry" would be undefined.

You've selected mode 2, at this point it doesn't know what dat_entry is:

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

You should declare dat_entry somewhere in your main loop or somewhere here once the user has selected option 2:

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

Upvotes: 0

Related Questions