Local variables are being used before assignment, but are assigned before used

Question:

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?
Asked By: Luke Martin

||

Answers:

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.

Answered By: Barmar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.