0

I'm trying to keep a count of the amount of times a random number is generated and stop it after a certain number has been hit. I'm new to python there's probably a simple fix that I can't see.

p = 0
def gameplay():
   i = random.randint(1,2)
   if i <= 1:
       print(1)
       p = p + 1
       gameplay();
   else:
      print(2)
      p = p + 1
      gameplay();


if p <= 10:
   gameplay();

I keep getting the error: "local variable 'p' referenced before assignment"

Dylan
  • 1
  • 1
  • 2
    Please fix your indentation, it's crucial to Python code. At the moment, we don't know what's contained within the function or what exists in your `if`/`else` blocks. – roganjosh Dec 11 '18 at 14:38
  • Possible duplicate of [Short Description of the Scoping Rules?](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – Azat Ibrakov Dec 11 '18 at 14:44
  • `p` does not exist in the scope of the function block. You _could_ declare it as `global p` at the top of the function but globals should generally be avoided (maybe pass as a parameter to the function). That said, this will catapult you right into a recursion error; it's not clear why you're calling the function recursively – roganjosh Dec 11 '18 at 14:44
  • You change `p` within your function gameplay. But p isn't returned in your function, nor is it a global variable. Therefor changing `p` within gameplay doesn't work. – Niels Henkens Dec 11 '18 at 14:44

1 Answers1

-3
p = 0
def gameplay(p):
   i = random.randint(1,2)
   if i <= 1:
       print(1)
   else:
      print(2)
  p = p + 1
  return p


while p <= 10:
   p = gameplay(p)

I edited your code, so that p is incremented in the gameplay-function and then returned to the variable 'p' again. Thereby eliminating your error and the need for a global variable p.

I changed the if-statement to a while loop, because I thought that the sample code tried to implement a recursive function that ran until p was larger than 10. If that's not what you want, just change it back to an if-statement, but then make sure p gameplay(p) gets called somewhere else in your code.

Niels Henkens
  • 2,553
  • 1
  • 12
  • 27
  • Your correct about the typo in `return` and the lack of explanation (although I thought it was pretty self-explanatory after my comment earlier). I don't get your comment about the while-loop not ending. (After fixing the return-typo) p gets incremented every run of gameplay, so it ends after 10 executions... – Niels Henkens Dec 11 '18 at 16:03
  • Thanks I tried out the new code and 1 and 2 were shown once when it ran and then I got the error "TypeError: '<=' not supported between instances of 'NoneType' and 'int'" – Dylan Dec 12 '18 at 01:06
  • That sounds like your function gameplay() does not always return `p` (so you get None as a return value, which can't be compared with '10'). Did you implement the exact same code as my example? It sounds like the `return` statement might be in the wrong place in your code. – Niels Henkens Dec 12 '18 at 09:03