9 Replies - 1858 Views - Last Post: 20 February 2016 - 02:17 PM Rate Topic: -----

#1 aareleb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 16

Local Variable Referenced Before Assignment

Posted 20 February 2016 - 12:49 PM

Hello everyone,
I am new to programming and the forums, teaching my self python. I would like some assistance in a D20 dice rolling system, everything worked in my code until I put everything in to functions. Now I get an error "local variable 'selectamount' referenced before assignment".
I have images linked and code I do not need some one to write the code for me I just need help in understanding what I am doing wrong. Thank you in advanced for any and all help.
https://goo.gl/photo...z4FoFycr3Eh7fu6
https://goo.gl/photo...8tDM1PxYzoLtkm7
https://goo.gl/photo...jjxG65ZHBArmYNA

import random

#user input
def user_input():
    global selectdice
    selectdice = input("Select dice to use: d2, d4, d6, d8, d10, d12, d20, d100:  ")
    global selectamount
    selectamount = int(input("How may would you like to roll? 1-100: "))
    selectamount -= 1
    


#functions

user_input()
 

def ran_d2():
    d2 = random.randint(1, 2)
    print ("you roll a d2:", ">>>",d2,"<<<")
  
   
def ran_d4():
    d4 = random.randint(1, 4)
    print ("you roll a d4:", ">>>",d4,"<<<")
    

def ran_d6():
    d6 = random.randint(1, 6)
    print ("you roll a d6:", ">>>",d6,"<<<")

def ran_d8():
    d8 = random.randint(1, 8)
    print ("you roll a d8:", ">>>",d8,"<<<")


def ran_d10():
    d10 = random.randint(1, 10)
    print ("you roll a d10:", ">>>",d10,"<<<")


def ran_d12():
    d12 = random.randint(1, 12)
    print ("you roll a d12:", ">>>",d12,"<<<")
    
     
def ran_d20():
    d20 = random.randint(1, 20)
    print ("you roll a d20:", ">>>",d20,"<<<")


def ran_d100():
    d100 = random.randint(1, 100)
    print ("you roll a d100:", ">>>",d100,"<<<")
#if statments.


def main():

    if selectdice == "d2":
        ran_d2()
        while selectamount > 0:
            selectamount -= 1
            ran_d2()

    if selectdice == "d4":
        ran_d4()
        while selectamount > 0:
            selectamount -= 1
            ran_d4()
          
    if selectdice == "d6":
        ran_d6()
        while selectamount > 0:
            selectamount -= 1
            ran_d6()
          
    if selectdice == "d8":
        ran_d8()
        while selectamount > 0:
            selectamount -= 1
            ran_d8()
          
    if selectdice == "d10":
        ran_d10()
        while amount() > 0:
            selectamount -= 1
            ran_d10()
          
    if selectdice == "d12":
        ran_d12()
        while selectamount > 0:
            selectamount -= 1
            ran_d2()
          
    if selectdice == "d20":
        ran_d20()
        while selectamount > 0:
            selectamount -= 1
            ran_d20()
          
    if selectdice == "d100":
        ran_d100()
        while selectamount > 0:
            selectamount -= 1
            ran_d100()                                                                       
   
            
#user_input()
main()     

This post has been edited by macosxnerd101: 20 February 2016 - 12:58 PM
Reason for edit:: Renamed title to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Local Variable Referenced Before Assignment

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3178
  • View blog
  • Posts: 12,004
  • Joined: 03-December 12

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:08 PM

Don't use global anything. You pass the values of the variables needed to the functions that require them, it is called dependency injection.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6921
  • View blog
  • Posts: 28,607
  • Joined: 12-December 12

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:33 PM

Yes, avoid globals.

For your specific error, this
        while selectamount > 0:

is the first time (during code execution) that selectamount is referenced in your code, and it hasn't yet been assigned a value.
Was This Post Helpful? 0
  • +
  • -

#4 aareleb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 16

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:34 PM

Thanks for the reply astonecipher, I have adjusted my code and removed the global declaration, now I get a new error see attached image
https://goo.gl/photo...KFzs5DehTVAcDJ6

here is the new code:
import random

#user input
def user_input():
    selectdice = input("Select dice to use: d2, d4, d6, d8, d10, d12, d20, d100:  ")
    selectamount = int(input("How may would you like to roll? 1-100: "))
    selectamount -= 1
    


#functions

user_input()
 

def ran_d2():
    d2 = random.randint(1, 2)
    print ("you roll a d2:", ">>>",d2,"<<<")
  
   
def ran_d4():
    d4 = random.randint(1, 4)
    print ("you roll a d4:", ">>>",d4,"<<<")
    

def ran_d6():
    d6 = random.randint(1, 6)
    print ("you roll a d6:", ">>>",d6,"<<<")

def ran_d8():
    d8 = random.randint(1, 8)
    print ("you roll a d8:", ">>>",d8,"<<<")


def ran_d10():
    d10 = random.randint(1, 10)
    print ("you roll a d10:", ">>>",d10,"<<<")


def ran_d12():
    d12 = random.randint(1, 12)
    print ("you roll a d12:", ">>>",d12,"<<<")
    
     
def ran_d20():
    d20 = random.randint(1, 20)
    print ("you roll a d20:", ">>>",d20,"<<<")


def ran_d100():
    d100 = random.randint(1, 100)
    print ("you roll a d100:", ">>>",d100,"<<<")
#if statments.


def main():

    if selectdice == "d2":
        ran_d2()
        while selectamount > 0:
            selectamount -= 1
            ran_d2()

    if selectdice == "d4":
        ran_d4()
        while selectamount > 0:
            selectamount -= 1
            ran_d4()
          
    if selectdice == "d6":
        ran_d6()
        while selectamount > 0:
            selectamount -= 1
            ran_d6()
          
    if selectdice == "d8":
        ran_d8()
        while selectamount > 0:
            selectamount -= 1
            ran_d8()
          
    if selectdice == "d10":
        ran_d10()
        while amount() > 0:
            selectamount -= 1
            ran_d10()
          
    if selectdice == "d12":
        ran_d12()
        while selectamount > 0:
            selectamount -= 1
            ran_d2()
          
    if selectdice == "d20":
        ran_d20()
        while selectamount > 0:
            selectamount -= 1
            ran_d20()
          
    if selectdice == "d100":
        ran_d100()
        while selectamount > 0:
            selectamount -= 1
            ran_d100()                                                                       
   
            
#user_input()
main()     

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6921
  • View blog
  • Posts: 28,607
  • Joined: 12-December 12

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:37 PM

That screenshot cannot be read easily. Post the error details directly here, they need to be available directly in the thread. See my previous post as well.

Have you just removed the global statements? That alone won't fix the code.
Was This Post Helpful? 0
  • +
  • -

#6 aareleb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 16

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:41 PM

View Postandrewsw, on 20 February 2016 - 01:33 PM, said:

Yes, avoid globals.

For your specific error, this
        while selectamount > 0:

is the first time (during code execution) that selectamount is referenced in your code, and it hasn't yet been assigned a value.


selectamount is declared on line 6.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6921
  • View blog
  • Posts: 28,607
  • Joined: 12-December 12

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:48 PM

Oh right, and you call user_input before main. So, sorry, the error isn't for the specific reason I suggested.

Still, now that you are taking a different approach, without globals, the code needs revising.
Was This Post Helpful? 0
  • +
  • -

#8 aareleb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 16

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:52 PM

Sorry I was thinking screen shots would make things easier. The error I get now is "name 'selectdice' is not defined". But selectdice is defined on line 5 and select amount is defined on line 6.
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3178
  • View blog
  • Posts: 12,004
  • Joined: 03-December 12

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 01:59 PM

You are still dealing with a scope issue. To make it a little more clear:


def add():
  print( a + b )

def main():
  a = 1
  b = 2
  add()



a and b exist in local scope. So, in the add function a and b are undefined, they do have values in main, however. To remedy that you would do this, it also relates to my previous post:

def add(a, b ):
  print(a + b )

def main():
  a = 1
  b = 2
  add(a, b )



You are now sending the values of a and b to add. add receives them and can utilize them. Typically, that function would then send them back to the calling function:

def add(a, b ):
  return a + b

def main():
  a = 1
  b = 2
  print( add(a, b ))


Was This Post Helpful? 1
  • +
  • -

#10 aareleb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 16

Re: Local Variable Referenced Before Assignment

Posted 20 February 2016 - 02:17 PM

Thank you for the help I am going to try your suggestions myself for a while. Thank you everyone for your input will post once get it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1