Why does the following code raise an UnboundedLocalError: n = 0 def foo():     n = n + 1     print(n) foo() foo()  Error: Traceback (most recent call last):   File &quot;foo.py&quot;, line 5, in &lt;module&gt;     foo()   File &quot;foo.py&quot;, line 3, in foo     n = n + 1 UnboundLocalError: local variable 'n' referenced before assignment  While the following code works: n = 0 def foo():     print(n) foo() foo()  Output: 0 0  It seems to me that given the behavior of the second snippet, the first snippet should use the value of the global n to calculate n+1 and assign it to a new local n, and print it. Thus, I would expect the output to be two 1s. Why does this not happen?
The following code works as expected in both Python 2.5 and 3.0:  a, b, c = (1, 2, 3)  print(a, b, c)  def test():     print(a)     print(b)     print(c)    # (A)     #c+=1       # (B) test()   However, when I uncomment line (B), I get an UnboundLocalError: 'c' not assigned at line (A). The values of a and b are printed correctly. This has me completely baffled for two reasons:   Why is there a runtime error thrown at line (A) because of a later statement on line (B)? Why are variables a and b printed as expected, while c raises an error?   The only explanation I can come up with is that a local variable c is created by the assignment c+=1, which takes precedent over the "global" variable c even before the local variable is created. Of course, it doesn't make sense for a variable to "steal" scope before it exists.  Could someone please explain this behavior?
Difference between $\sum_{i=1}^{k}{\frac{s_i}{kn_i}}$ and $\frac{\sum_{i=1}^{k}{s_i}}{\sum_{i=1}^{k}{n_i}}$ I am getting confused at how to calculate the average probability.  Suppose we repeat a kind of binary survey $k$ times each of which was done on a completely separate sample group.  For each $i^{th}$ group, $i=1,2,3,...,k$, Let $n_i$ be the number of samples and $s_i$ be the number of positive results. With this we know that the probability $p_i$ of the positive result for the $i^{th}$ group is $s_i/n_i$  With my ignorance, I happened to use two ways to calculate the average probability in my work wrongly assuming that they are the same:  $$\frac{p_1+p_2+p_3+...+p_k}{k}$$ and $$\frac{s_1+s_2+s_3+...+s_k}{n_1+n_2+n_3+...+n_k}$$  I don't know which is the correct way to calculate the average probability. So could you please explain the difference of these two and when to use which? Prove $\frac{1}{2}\sum_p \frac{1}{p^2}+\frac{1}{3}\sum_p \frac{1}{p^3}+\cdots$ converges Java Enum definition My machine is currently running Windows 7. I have 2 partitions: OS (C:) and DATA (D:). I intend to do a native Ubuntu 14.04 install. Assuming I format my Windows OS partition, will I have full access to my D partition in Ubuntu ? PhD duration in the USA I am trying to find out what the average duration of PhD studies are in the USA. Here in Spain it used to be 4 years, but it seems that Europe is moving towards a 3-year scheme since students are expected to do their MSc before they start. What is it like in the USA? Is some kind of shortening also occurring? Horizontal Probability Tree with Level Labels I'm trying to make a basic Windows application that builds a string out of user input and then adds it to the clipboard. How do I copy a string to the clipboard using Python? How do I turn a String into a InputStreamReader in java? How can I transform a String value into an InputStreamReader? I'm trying to make a quicklist on the Workspace Switcher launcher that allows for quick switching to a certain workspace. (My friend sometimes uses my computer and just doesn't enjoy keyboard shortcuts, so I want to help him out.)  So is there a command one could type in a terminal that would force a certain workspace to be focused on? How does the SQL injection from the "Bobby Tables" XKCD comic work? Just looking at:   (Source: )  What does this SQL do:  Robert'); DROP TABLE STUDENTS; --   I know both ' and -- are for comments, but doesn't the word DROP get commented as well since it is part of the same line? Integrating $\int_0^1 \frac {\log(1-x)\log^2(1+x)}x \mathrm{d}x$ Question: Integrate$$\int\limits_0^1dx\,\frac {\log(1-x)\log^2(1+x)}x=-\frac {\pi^4}{240}$$   I'm curious as to if there is a way to integrate this. I've tried using integration by parts to get$$I=-\frac {\pi^2}6\log^22+2\int\limits_0^1dx\,\frac {\operatorname{Li}_2(x)\log(1+x)}{1+x}$$However, I'm not sure how to continue even further. The polylog in the second integrand seems a bit intimidating and I don't see how the first term even helps. Voting on Meta sites generally express agreement and disagreement with a proposal, unlike on the Q&amp;A sites where they indicate useful/good vs. non-useful/bad content.  It seems to me that a down-vote on Meta Stack Exchange should not carry a -1 rep penalty, which only discourages down-voting. With this discouragement, questions on Meta SE will be biased towards agreement, since for every up-voter, there may be several in disagreement who don't want to lose the rep for down-voting.  Can the penalty for down-voting on Meta SE be removed? When is the graph of the divisor relation on $[n]$ planar? What are your thoughts on writing your text in images?  I'm not saying throughout the whole website... But in certain key locations like image carousels. And I'm not talking about the special cases where you have font types that browsers can't render. I'm talking about text that uses standard fonts and is not too big (3/4 bullets).  Should I embed the text in the images that compose the carousel? Or should I apply the text to the images as "real text" HTML?  What are the advantages/ disadvantages of using text in an image? Windows 10 sees much more monitors than I have How do I prevent Apache from answering requests for domains I don't host? Simple image editor? show that the interval of the form $[0,a)$ or $(a, 1]$ is open set in metric subspace $[0,1]$ but not open in $\mathbb R^1$ On the metric subspace $S = [0,1]$ of the Euclidean space $\mathbb R^1 $, every interval of the form $A = [0,a)$ or $(a, 1]$ where $0&lt;a&lt;1$ is open set in S. These sets are not open in $\mathbb R^1$   Here's what I attempted to show that $A$ is open in $S$. I have no idea how it is not open in $\mathbb R^1$.  Let $M = \mathbb R^1$, $x \in A  = [0,a)$ . If $x = 0$, $ r \leq \min \{a, 1-a\}, \\\  B_S(0; r) = B_M(0,r)\cap[0,1] = (-r, r) \cap[0,1] = [0,r) \subseteq A $  If $x \neq 0, r \leq \min \{x,|a-x|, 1-x\},  \\B_S(x; r) = B_M(x,r)\cap[0,1] = (x-r, x+r) \cap[0,1] \subset (0,x) \text{ or } (x, 1) \subset A$ Hahn-Banach Theorem for separable spaces without Zorn's Lemma Full Name Requirement of Commit Phase The Commitment form on Area 51 requires your full name to be provided. Is this publicly viewable? Why not just use the existing usernames?