Python Forum
Cant access variable from anywhere
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant access variable from anywhere
#11
Quote:Already try something like that,
You need to be more specific. I mentioned several things in my prior post. Which of them did you try? A loop? A function? A class?

I don't understand what you mean by "recycle the variable Z value".
Z = 0
for i in range(2):
    print(Z)
    Z = Z + 1
Output:
0 1
Look at that! I just "recycled" the variable Z value.

What do you want to do that is different from that?
Reply
#12
You didnt recycle the value of the variable because Z is not use outside of the scope of the function.

Lets try this:

cma = 0 <------- if equal 0 cma will return 0 not a new value



Now i want the result of cma introduce in fibo formula but the fibo variable is on top of it and will not recycle the value. Sorry maybe my explaination is wrong. Suppose cma is meant to return 3218. 3218 should end up in fibo but 0 take the place instead.
Reply
#13
Not that it matters, but the code you reference is not in a function. The scope of Z is global and it can be seen by everyone. But I think this entire conversion has been talking about the wrong issue. The problem is not that you can't access a variable, because clearly you can. The problem is that changing the value of a variable is not changing the value of other variables that were calculated using that variable. If A = B + C, changing B or C does not automatically update the value of A. That is not the way variables work. But that is the way functions work.

If want to recalculate fibo using the current value of cma. Why not make it a function and call the function each time you want the current value?
def fibo():
    return (max_close - (3853.5)) / (max_close - cma)
Then anywhere you want to know the value of fibo you just call fibo().

If you don't re-evaluate the equation with updated values how do you expect the result to change? And contrary to your claims, C# would not fix the problem either. C# variables don't automatically update their values. You can make a property in C# that looks like a variable but is actually a function. When you reference the variable, it executes the function and returns the value. You can do the same thing with Python, and these function-like variables in Python are also called properties.
Reply
#14
I did that also but it still returning 0.


I could even include the foundIndex loop in a function it will still return 0
Quote:The problem is that changing the value of a variable is not changing the value of other variables that were calculated using that variable
You are right, from my point of view it looks like the value cant be reach. But in C# there is no function, no methods, just loops and it work.
Reply
#15
From your latest post it looks like there is another loop that you are not showing (index). Is all the code from your last post inside another loop? If so, there is no need to make fibo a function. It will update each time the loop executes.
cma = 0
for index in range(whatever):
    fibo = (max_close - (3853.5)) / (max_close - cma)  # Will use current cma value, not always zero.
    sumvolfibo = sm / fibo()

    foundIndex = 0
    som = 0
    for i in range(index,810):
        som += vol_list[i]
        if som >= sumvolfibo:
            foundIndex = i
            break

    # do something that calculates a new cma value
Reply
#16
Yes the index part is inside another loop. With or without a function for any loop i can get cma to return the value it suppose to be. I tried to move cma all around the place and it still return 0. I interchange the function or the loops and cma must be declare before one of the function. I am out of idea.
Reply
#17
Works fine for me.
cma = 0
max_close = 10000
for index in range(10):
    fibo = (max_close - (3853.5)) / (max_close - cma)  # Will use current cma value, not always zero
    print(fibo, cma)
    cma = cma + 100
0.61465 0
0.6208585858585859 100
0.6271938775510204 200
0.6336597938144329 300
0.6402604166666667 400
0.647 500
0.6538829787234043 600
0.6609139784946236 700
0.6680978260869566 800
0.6754395604395604 900
Of cource cma may not change based on the equations used to calculate cma.
cma = 0  # <- Assigned outside any loop
max_close = 10000
for index in range(10):
    fibo = (max_close - (3853.5)) / (max_close - cma)  # Will use current cma value, not always zero
    print(fibo, cma)
    cma = cma **2
Output:
0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0 0.61465 0
If you initialize cma = 0 outside of any loop and assign a different value to cma inside the loop, fibo will change value.
Reply
#18
Its working because you included fibo inside the loop.
You can integrate fibo and sumvolfibo in index loop or foundindex loop and even integrate index loop inside foundIndex. My point there will be always one that will end up outside de loop and will need to be declare outside producing a 0 return, if its not sumvolfibo, it will be fibo or foundindex. Python complicate things, i dont have that problem with c#.
Reply
#19
Of course I include the fibo calculation in the loop. How do you expect the fibo value to change if you don't recalculate the value?

C# works exactly the same way. If you don't agree, provide an example in C#. If I can't do the exact same thing with Python I'll concede. The two languages are very similar other than Python being much less restrictive.
Reply
#20
Sorry i dont want to post the code here. I even wonder if its possible to erase your post. I posted enough, ill try to find a similar exemple. In C# i have:

First loop going from foundIndex to the beginning of the series of data.
Second loop that goes from Index to the begenning of data series.

fibo is outside between first and second, so is sumvolfibo = sm / fibo

Most are local variables declared just above the loop like sum and vol. Except foundIndex, Index, cma, sumvolfibo who are private double variable.

Keep in mind all for loops are independant not nested.

Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can we access instance variable of parent class in child class using inheritance akdube 3 14,027 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Access a variable of a daemon peek_no_boo 8 3,403 Apr-03-2020, 07:29 PM
Last Post: BrendanD
  How to access class variable? instances vs class drSlump 5 3,381 Dec-11-2019, 06:26 PM
Last Post: Gribouillis
  How can I access this variable from a def? student3m 1 2,955 Sep-23-2017, 02:06 PM
Last Post: ichabod801
  Can access class private variable? Michael 2 7,205 Aug-11-2017, 01:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020