3 Replies - 1110 Views - Last Post: 07 May 2016 - 04:19 PM Rate Topic: -----

#1 kobs20   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-May 16

Error: "Referenced before assignment" - Dice Game

Posted 07 May 2016 - 01:19 PM

 #Jacob Kobs
#May 7th, 2016
#Roll Dice Program
#This program is designed to have 2 people virtually roll a dice(1, 6) and
#whoever has the higher number wins.
#--------------------------------------------------------------------------

#Lab 7-3 The Dice Game
#add libraries needed
import random
#the main function
def main():
    print
    #initialize variables
    endProgram = 'no'
    playerOne = 'NO NAME'
    playerTwo = 'NO NAME'

    #call to inputNames
    playerOne, playerTwo = inputNames(playerOne, playerTwo)

    #while loop to run program again
    while endProgram == 'no':

        #initialize variables
        winnersName = 'NO NAME'
        p1number, p2number = 0, 0

        #call to rollDice
        winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)

        #call to displayInfo
        displayInfo(winnerName)

        endProgram = raw_input('Do you want to end program? (Enter yes or no): ')


   	#this function gets the players names
def inputNames(playerOne, playerTwo):
    playerOne = raw_input("What is the name of player one? ")
    playerTwo = raw_input("What is the name of player two? ")
    return playerOne, playerTwo

#this function will get the random values
def rollDice(p1number, p2number, playerOne, playerTwo, winnerName):
    p1number = random.randint(1, 6)
    p2number = random.randint(1, 6)
    if (player1number > p2number):
        print playerOne(winnername)
    elif (player1number < p2number):
        print playerTwo(winnerName)
    else:
        print "TIE"
    return winnerName

#this function displays the winner
def displayInfo(winnerName):
    print winnerName


# calls main
main() 


This is the error I am getting:
File "C:/Users/jkobs/Desktop/Assignments for college and programs/Programming Fundamentals/Module 05- Week 05/Lab7-3a.py", line 30, in main
    winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)
UnboundLocalError: local variable 'winnerName' referenced before assignment


I have tried to make winnerName a global variable, but received an error after doing so. All help is appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Error: "Referenced before assignment" - Dice Game

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

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

Re: Error: "Referenced before assignment" - Dice Game

Posted 07 May 2016 - 01:24 PM

        winnersName = 'NO NAME'

Is this supposed to be winnerName? If not then it is not a good idea to have two variables with such similar names. Otherwise, yes, you need to assign an initial value to winnerName before you can reference it.

If you have any further questions concerning this game then please add them here, there is no need to create a new topic for every individual question about this.
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: Error: "Referenced before assignment" - Dice Game

Posted 07 May 2016 - 01:31 PM

Also, note that your rollDice function will return the same winnerName value each time. So either there is no point in passing this value to the function or the function should return a different value (a different winner's name) depending on the outcome.
Was This Post Helpful? 0
  • +
  • -

#4 kobs20   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-May 16

Re: Error: "Referenced before assignment" - Dice Game

Posted 07 May 2016 - 04:19 PM

That is how my professor has worded it. He probably made winnersName and winnerName separate because one of the two holds a string, which is NO NAME. I'm so lost. I feel like I am close to the finish line, though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1