0

I'm trying to put some code into a function but I get an Unbound Local Error - I sort of understand why, I just don't know how to fix it.

edit: specifically I was told not to use global because global "is a bad idea". Surely there must be another way?

In the overall code I have multiple variables which need to be altered by user_input and those altered values then need to be passed through one or more different functions (repeatedly (with new values) until a certain conclusion is drawn).

How can I use the variables in multiple functions (cleanly)? If I define them in one function then they're only available there, right? And I can't alter their values from/to elsewhere?

Thank you for any help.

This is a sketch of the code I want to put in a function:

#variables that need to be used (and altered) within multiple functions
Var1 = #a_number
Var2 = #a_number
Var3 = #a_number

def a_function(): #created Unbound Local Error
    output() #prints original vars
    while True:
        inp = float(input("Enter a number: "))

        Var1 += inp
        Var2 -= inp
        Var3 *= inp
        output() #prints new values for vars

        if/elif/else....
        #when Vars are a certain number new calculations are done
        #on them. Regardless, there is always only one output of
        # the 3 Vars
        #some Var vals trigger certain functs which print those new value
        #or functions which may even manipulate the values further
CoderBaby
  • 91
  • 2
  • 8

1 Answers1

1

In your function, start with:

global Var1, Var2, Var3
René Pijl
  • 4,310
  • 1
  • 19
  • 25