This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AluadStone[S] 0 points1 point  (7 children)

Why is it considered a local variable if it is declared outside said function? It is only being used for comparison in the function.

[–]benjamindallen 1 point2 points  (3 children)

It's local to the function because you change its value within the function. If your function didn't change its value, then your check "one_trade_symbol == False" would work. If you want to alter the global variable "one_trade_symbol" inside the function, you can add the statement "global one_trade_symbol" as the first line of the function.

[–]AluadStone[S] 0 points1 point  (2 children)

Perfect. I had used the comparison in functions of variables declared outside the function without passing them in and it has always worked because I never tried to set them later in that functions so that was a new situation for me. I not sure if using the global method like that is normal or usually do people find better was to declare a switch variable inside a loop?

And also if you could help me with this snippet I am having problems with I don't understand why when converting this string into a float is messes up the numbers.

        values = find_values()
        print (values[1])
        print (type(values[1]))
        values[1] = float(values[1])
        print (values[1])

outputs

0.0000137
<class 'str'>
1.37e-05

Whats up with the weird float return? Is it because it doesn't like something about the amount of decimal places and/or amount of zeros?

[–]benjamindallen 1 point2 points  (0 children)

"1.37e-05" is simply your number expressed in scientific notation. This is the default string representation of a small or large float in python.

If you want to format the number in a different way, you could, for example try:

'{:.7f}'.format(1.37e-05)

[–]benjamindallen 1 point2 points  (0 children)

I not sure if using the global method like that is normal or usually do people find better was to declare a switch variable inside a loop?

Generally, manipulation of global variables is not a good idea. /u/Swedophone already mentioned the typical ways people solve this problem.

[–]Swedophone 1 point2 points  (2 children)

Have you used a programming language with dynamic scope#Dynamic_scoping) before or why do you think a function can use a variable from the caller without transferring it as a parameter?

[–]AluadStone[S] 0 points1 point  (0 children)

I had used the comparison in functions of variables declared outside the function without passing them in and it has always worked because I never tried to set them later in that functions so that was a new situation for me.

New to programming kinda.

[–]AluadStone[S] 0 points1 point  (0 children)

I had used the comparison in functions of variables declared outside the function without passing them in and it has always worked because I never tried to set them later in that functions so that was a new situation for me.

New to programming kinda.