(Sep-28-2018, 05:14 PM)ichabod801 Wrote:(Sep-28-2018, 04:35 PM)WuchaDoin Wrote: How would you use a variable globally without being global?
With parameters and return values:
>>>> def add2(x): ... return x + 2 ... >>> z = 5 >>> z = add2(z) >>> z 7Also with classes, which can retain values within an instance of the class. There are links to tutorials for both in my signature below.
With posting this example, I've noticed the problem I keep encountering is that looks like it calls for global variables. Here is how it is setup and why I am having to use global variables. I know there is a work around which is why I am posting the snippet.
First I have main called:
def Main(): CustomerSaleID = CustomerSaleOBJID() Vin = FindVin() FPnum = FindFPnum()Then each function is called in the proper order in Main().
def FindVin(): Vin = str(CustomerIndex[int(float(LoadedSales['CC'][InxLoc]))][2])[-8:] return Vin def FindFnum(): Fnum = str(CustomerIndex[CustomerSaleID][3]) return Fnum def CustomerSaleOBJID(): CustomerSaleID = int(float(LoadedSales['CC'][InxLoc])) return CustomerSaleIDWhen the code reaches FindFnum() and has to use the variable CustomerSaleID (which should already be declared by then); This is where I begin to encounter my problem. I need to use CustomerSaleID in multiple functions, but using what it shown above; it seems to create a local variable? Vin, CustomerSaleID, and FPnum will be use throughout all 13 functions. Would the workaround being that I would have to declare a local variable to be used within each function?
i.e.
def FindFnum(): CustomerSaleID = CustomerSaleOBJID() Fnum = str(CustomerIndex[CustomerSaleID][3]) return Fnum