I was writing a small game in which you would guess a number between 0 and 100 in your mind and the computer will try to guess it. Here is a snippet of the code:
import random
global foo
foo=input()
global k
k=random.randint(0,100)
def f():
if foo.lower()=='too low':
k=random.randint(k,100)
foo=input('The number that I guessed is' + ' ' + str(k) +'. Please give your comment.')
print(k)
f()
It throws an error saying:
UnboundLocalError: local variable ‘foo’ referenced before assignment
Other posts on this site suggest to use global
. I did and am still getting an error. Why is Python saying that foo
is a local variable even when I have declared it global? And How do I get rid of this bug?