Why is a global variable not accessible from a function?
📁 функция, python, ии
Please tell me why the function does not output the value of s to the console
s = ""I'm global variable""def foo(): print(s) s = 'Reset global variable'foo()I assumed that when the interpreter reaches the line print(s), it would understand that there is no such variable in the scope of the foo function. Consequently, it would start looking for it in the outer scope, where it exists.
However, as a result, the following error message is displayed in the console:
Traceback (most recent call last): File ""index.py"", line 7, in foo() File ""index.py"", line 4, in foo print(s)UnboundLocalError: local variable 's' referenced before assignment Why does this happen? After all, the variable s is not defined inside the function, so it should be taken from the global scope.
There is a separate algorithm for finding the period of a rational number. We iterate through powers of 10 one by one: 10, 100, 1000, 10000, etc. We look at the remainder of dividing this number by the denominator. If the remainder equals 1, then that power of 10 is the length of the period. For example, if the denominator is 13, then:
10 % 13 = 10100 % 13 = 91000 % 13 = 1210000 % 13 = 3100000 % 13 = 41000000 Log in to leave an answer