Python Return Statement Not Providing Expected Results

Question:

Just attempting to return values from a defined function. When calling the function first and attempting to print the return values I receive "[variable] not defined". However, if I run "print(qb_stat_filler())" it prints the results in a tuple. I need the individual variables returned to use in a separate function.

For Example
print(qb_stat_filler())
outputs: (0, 11, 24, 24.2024, 39.1143, 293.0, 1.9143000000000001, 0.2262, 97.84333355313255)

but when trying
qb_stat_filler()
print(cmp_avg)
print(att_avg)
outputs: NameError: name ‘cmp_avg’ is not defined

Process finished with exit code 1

I’ve tried establishing the variables outside of the function, then passing and returning them and that did not work either. Any thoughts?

        def qb_stat_filler():
                n_input = input('Player name: ')
                t_input = input('Players team: ')
                loc_input = input('H or @: ')
                o_input = input('Opponent: ')

                # convert index csv to dictionary of player values
                q = pd.read_csv('Models\QB Indexes\QBname.csv')
                q = q[['Player', 'Num']]
                qb_dict = dict(q.values)
                name = qb_dict.get('{}'.format(n_input))

                t = pd.read_csv('Models\QB Indexes\Tmname.csv')
                t = t[['Tm', 'Num']]
                tm_dict = dict(t.values)
                team = tm_dict.get('{}'.format(t_input))

                loc = 0
                if loc_input == '@':
                loc = 0
                elif loc_input == 'H':
                loc = 1

                z = pd.read_csv('Models\QB Indexes\Oppname.csv')
                z = z[['Opp', 'Num']]
                opp_dict = dict(z.values)
                opp = opp_dict.get('{}'.format(o_input))
                *there are several lines of code here that involve SQL 
                queries and data cleansing*
                cmp_avg = (cmp_match + cmpL4) / 2
                att_avg = (patt_match + pattL4) / 2
                pyds_avg = (py_match + pydsL4) / 2
                ptd_avg = (ptdL4 + ptd_match) / 2
                int_avg = (intL4 + int_match) / 2
                qbr_avg = (qbr_match + qbrL4) / 2
                return name, team, opp, cmp_avg, att_avg, pyds_avg, ptd_avg, 
                int_avg, qbr_avg


        qb_stat_filler()
Asked By: Chup91

||

Answers:

You might consider:

def qb_stat_filler():
    stats = {}
    ...
    stats['name'] = name
    z = z[['Opp', 'Num']]
    opp_dict = dict(z.values)
    stats['opp'] = opp_dict.get('{}'.format(o_input))
    ...
    stats['cmp_avg'] = (cmp_match + cmpL4) / 2
    stats['att_avg'] = (patt_match + pattL4) / 2
    stats['pyds_avg'] = (py_match + pydsL4) / 2
    stats['ptd_avg'] = (ptdL4 + ptd_match) / 2
    stats['int_avg'] = (intL4 + int_match) / 2
    stats['qbr_avg'] = (qbr_match + qbrL4) / 2
    return stats
...
stats = qb_stat_filler()
print(stats['cmp_avg'])
Answered By: Tim Roberts