0

I'm trying to improve my coding by using dictionaries.

I have one with inputs and one with outputs. The problem is that is seems like I can't reference to a dictionary inside said dictionary.

def calculate(x,y):
    return x + y

inputs = dict(
              a = 1,
              b = 2,
              c = 3,
              )

outputs = dict(
               d = calculate(inputs['a'], inputs['b']),
               e = calculate(inputs['a'], outputs['d']),
              )

e is creating trouble.NameError: name 'outputs' is not defined

Should I use self of some sorts here?

Bonus question:

I have about 30 inputs, and 20 output calculations. Do you recommend another way of solving this than the way I have?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
tore
  • 303
  • 3
  • 11

1 Answers1

2

Couple of points here:

  • Check your brackets (e.g. last two lines with calculate(...))
  • outputs is not defined when accessing it with outputs['d'] (last line, see javo's answer)
  • I don't see the advantage of dicts over variables in this scenario

Long story short:

def add(x,y):
    return x + y

a = 1
b = 2
c = 3

d = add(a, b)
e = add(a, d)
Matt
  • 17,290
  • 7
  • 57
  • 71
  • Oh, ok. I can live with this. Thank you :) I want to use dicts because later I will loop through the container and write an output file. In total, I might have about 50 variables. – tore Dec 02 '13 at 10:22
  • With 50 variables it's probably a diffrent story. What is it exactly that you're trying? (Maybe you can use lists?) – Matt Dec 02 '13 at 10:58