0

I am getting this error message:

   Traceback (most recent call last):
      File "python", line 256, in <module>
      File "python", line 11, in questions
      File "python", line 190, in instructions
      File "python", line 27, in names
      File "python", line 45, in p1turn_part_1
      File "python", line 137, in p2turn
      File "python", line 86, in p1turn_part_2
    UnboundLocalError: local variable 'p1point' referenced before assignment

from This Website. The code is:

import time
import random


###########
#Functions#
###########
def questions():
  instruction_or_skip = input('Type \'instructions\' to learn how to play. If you don\' want to learn, type \'skip\'')
  if instruction_or_skip == 'instructions':
      instructions()
  elif instruction_or_skip == 'skip':
      names()
  else:
      print('\nSorry, I didn\'t get that.\n')
      questions()

#--starting screen--#
def startScreen():
    loadingScreen()
    
def names():
  global p1name
  p1name = input('What is the first player\'s name?')
  global p2name
  p2name = input('What is the second player\'s name?')
  p1turn_part_1()
    
def p1turn_part_1():
    global rounds
    print('\nHow many rounds?')
    rounds = input(':>')
    rounds = int(rounds)
    global p1point
    p1point = 0
    p1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
    p1 = (random.choice(p1))
    print p1
    if p1 == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9':
      p1point = p1point + 1
      time.sleep(1)
      global rounds
      rounds = rounds - 1
      print(p1name + ' gets a point!')
      p2turn()
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
    else:
      time.sleep(1)
      print(p1name + ' gets no points...')
      p2turn()
      global rounds
      rounds = rounds - 1
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
      else:
        p2turn()
        
def p1turn_part_2():
  #1-9 are divisble by 4
  #10-27 are divisble by 2
    p1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
    p1 = (random.choice(p1))
    print p1
    if p1 == '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9':
      p1point = p1point + 1
      time.sleep(1)
      global rounds
      rounds = rounds - 1
      print(p1name + ' gets a point!')
      p2turn()
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
    else:
      time.sleep(1)
      print(p1name + ' gets no points...')
      global rounds
      rounds = rounds - 1
      p2turn()
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
      else:
        p2turn()
      
def p2turn():
    global p2point
    p2point = 0
    p2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36']
    p2 = (random.choice(p2))
    print p2
    if p2 == '10' or '11' or '12' or '13' or '14' or '15' or '16' or '17' or '18' or '19' or '20' or '21' or '22' or '23' or '24' or '25' or '26' or '27':
      global p2point
      p2point = p2point + 1
      time.sleep(1)
      global rounds
      rounds = rounds - 1
      print(p2name + ' gets a point!')
      p1turn_part_2()
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
    else:
      time.sleep(1)
      global rounds
      rounds = rounds - 1
      print(p2name + ' gets no points...')
      p1turn_part_2()
      if rounds < 0:
        print p1point
        print p2point
        if p1point < p2point:
          print('\nPlayer 2 wins!')
          exit(0)
        elif p2point < p1point:
          print('\nPlayer 1 wins')
          exit(0)
        else:
          print('It\'s a tie!')
          exit(0)
      else:
        p1turn_part_2()




      

#--the instructions--#
def instructions():
    print('''             ##########################
             #########DIE ROLL######### 
             ##########################''')
    print('\nRules:')
    print('\n1.)Follow all prompts on screen')
    print('2.)Do not modify the script under ANY curcumstances')
    print('3.)You do not need to write down any scores or round numbers,')
    print('   They will be recorded for you.')
    print('4.)P1 needs to get any number divisable by 4')
    print('5.)P2 needs to get any number divisable by 2')
    print('6.)P3 needs to get any number divisable by 3')
    print('7.)P4 needs to get any number divisable by 5')
    names()
#--for looks only--#
def loadingScreen():
    print('\n' * 100)
    print('\nLoading...')
    print('\n')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading.')
    print('\n-')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading..')
    print('\n-')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading...')
    print('\n--')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading.')
    print('\n--')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading..')
    print('\n---')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading...')
    print('\n---')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading.')
    print('\n----')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading..')
    print('\n----')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading...')
    print('\n-----')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading.')
    print('\n-----')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading..')
    print('\n------')
    time.sleep(0.5)
    print('\n' * 100)
    print('\nLoading...')
    print('\n------')
    time.sleep(0.5)
    
    

    
    
    
############
####GAME####
############
print('test complete')
startScreen()
questions()
global rounds
print('\nHow many rounds?')
rounds = input(':>')
rounds = int(rounds)

Does anyone know what the problem is? (this also applies to the p2point and rounds variables.)(test on previous site.)

Side questions:

  1. Why does player 1 get a point no matter what number is taken?
  2. Will an infinite loop be created by this program?
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    There are about a million questions on this exact same topic. You should do some research before posting a new question. – juanpa.arrivillaga Jun 14 '17 at 23:23
  • Add `global p1point` to `p1turn_part_2` – Barmar Jun 14 '17 at 23:24
  • Here is your issue: `p1point = p1point + 1` – juanpa.arrivillaga Jun 14 '17 at 23:24
  • see the thing is, I did do research and found some options which I either executed poorly or didn't work, and it was probably a mix of both – Luke Martin Jun 14 '17 at 23:24
  • Thanks, but now It's an infinite loop. oops. – Luke Martin Jun 14 '17 at 23:27
  • @DaniSpringer No it's not. Primarily because Python does not have variable declarations. But regardless, that is the source of the error in that function (`p1turn_part_2`), and the solution is as Barmar said. – juanpa.arrivillaga Jun 14 '17 at 23:28
  • @DaniSpringer No, *look in the function*. The variable `p1point` is **never assigned to** before `p1point = p1point + 1`. The latter expression is then evaluated with the compiler having marked `p1point` as a local variable, and then it tries to reference itself in the right-hand side of the statement, and the error is thrown. This is a very common beginner's mistake. Like I stated, there are probably thousands of questions on the python tag asking the same thing. See the linked duplicate for more info. – juanpa.arrivillaga Jun 14 '17 at 23:32
  • I see `global p1point p1point = 0` before `p1point = p1point + 1`. Don't you? –  Jun 14 '17 at 23:35
  • @DaniSpringer Yes, in `p2turn`, **not** in `p1turn_part_2` where the error is being thrown, i.e. where it matters. – juanpa.arrivillaga Jun 14 '17 at 23:36
  • 1
    Got you now. I _am_ new to Python and stuff, but I did learn my scopes, so was quite confused. :D Thanks –  Jun 14 '17 at 23:38
  • @DaniSpringer Python is a simple language, but certain things don't work exactly like in other languages, but once you learn how it works, it's pretty simple to remember. But basically, as far as scope, outside of the global scope, if a variable is *assigned to ever* it is considered *local* **unless** you use the `global` directive. If you *really* want to go down the rabbit hole, check out [this](https://stackoverflow.com/questions/42451827/python-local-variable-compile-principle) question. – juanpa.arrivillaga Jun 14 '17 at 23:40
  • @DaniSpringer so basically `def func(): x = x + 1` *will always* throw an error, even if a global `x` exists, unless you put `global x` inside `func` – juanpa.arrivillaga Jun 14 '17 at 23:41
  • Right. By the way (though this may make look stupid, who cares), Python is my first language. :) –  Jun 14 '17 at 23:46
  • Just another question (hehe... :( ) Why does an infinite loop come afterwards? – Luke Martin Jun 14 '17 at 23:48
  • @juanpa.arrivillaga I read it. A rabbit hole indeed! Goodness... –  Jun 14 '17 at 23:48
  • 1
    @LukeMartin check this out: http://pythontutor.com –  Jun 14 '17 at 23:48
  • It dosen't appear to work... – Luke Martin Jun 14 '17 at 23:50

1 Answers1

0

p1turn_part_2 contains the following statement:

p1point = p1point + 1

In order to set the new value of p1point, it needs to use the old value. But the variable has never been set before, so it gets an error when trying to add 1 to the old value.

I assume this is supposed to be using the value that was set in p1turn_part_1. That function has:

global p1point

so it's setting the global variable. But since p1turn_part_2 has no such statement, it tries to use a local variable, not the global variable. Add the same global statement to p1turn_part_2.

Barmar
  • 741,623
  • 53
  • 500
  • 612