J.Riley
J.Riley

Reputation: 19

"Variable not defined in function" error

I have to make a dice game in Python, but I'm getting hung up on getting the functions to work properly.

from random import randint

def rollAll5Die():

    dice = []
    for c in range(0,5):
        dice.append(randint(1,6))

    return dice

def outputUpdate(oTitle, diceList):

    print(oTitle)
    print(diceList)
    print('')

def whichRoll():

    rollWhich = input("Enter dice/die you want to roll again--1 2 3 4 or 5: ")
    lRollAgain = rollWhich.split()
    print(lRollAgain)
    print('')


def rollSelected():

    for i in lRollAgain:
        rolledDice[int(i) - 1] = randint(1,6)


def dicePokerGame():

    keepPlaying = True

    while keepPlaying:

        isPlay = input('Please enter "y" if you want to play another round of the dice game or "q" if you want to quit game: ')
        print('')

        if isPlay == 'y':

            rolledDice = rollAll5Die()

            outputUpdate("Roll of five dice", rolledDice)

            nRollAgain = whichRoll()

            rollSelected()

            outputUpdate("Second Roll", rolledDice)

            nRollAgain = input("Enter Dice you want to roll for final roll: ")
            lRollAgain = nRollAgain.split()
            print(lRollAgain)
            print('')

            for i in lRollAgain:
                rolledDice[int(i) - 1] = randint(1,6)

            print("Final Roll")
            print(rolledDice)
            print('')


    #Score the hand Function

        counts = [0] * 7
        for value in rolledDice:
            counts[value] = counts[value] + 1

        if 5 in counts:
            score = "Five of a Kind", 30
        elif 4 in counts:
            score = "Four of a Kind", 25
        elif (3 in counts) and (2 in counts):
            score = "Full House", 15
        elif 3 in counts:
            score = "Three of a Kind", 10
        elif not (2 in counts) and (counts[1] == 0 or counts[6] == 0):
            score = "Straight", 20
        elif counts.count(2) == 2:
            score = "Two Pair", 5
        else:
            score = "No Winnings", 0

        print("Score")
        print(score)
        print('')


    else:

        keepPlaying = False

def main():

    dicePokerGame()

main()

When I run this program, it works properly until it gets to the function rollSelected() and gives me an error. The error says that lRollAgain is not defined in the rollSelected function.

Could somebody maybe explain why it says lRollAgain is not defined in the rollSelected functions? Because it is in the whichRoll function. I guess I just don't know what it's asking.

Upvotes: 0

Views: 80

Answers (2)

R Nar
R Nar

Reputation: 5515

It says that because IRollAgain is just a name for a variable. you declared it in the whichRoll function which means that it only means something in the whichRoll function. Since the rollSelected function is not in the whichRoll function, IRollAgain means nothing in rollSelected. You will have to declare it again as what you want it to be by passing in IRollAgain as an argument in the rollSelected function. ie

rollSelected(thisRoll)

EDIT:

I also think you mean your whichRoll function to look more like this:

def whichRoll():

  return input("Enter.... etc")

this way, you are returning the value that you input. I would also suggest a few checks to ensure the input is right

Upvotes: 0

Alfe
Alfe

Reputation: 59416

You are using your variables as if they are global. Albeit your syntax makes them local. (I propose to look up these terms.) As a result, they only exist within their narrow scope (e. g. the function they are used in). Setting a variable in one function and using it in another function does only work for global variables.

A quick solution would be to make all your variables global like this:

lRollAgain = None

def anyFunction():
  global lRollAgain
  # now use lRollAgain or assign a new value to it ...

A much better approach would be to learn about scopes, locals, and function arguments and return values. Then you should pass values from function to function via parameters and return values instead of doing so by global variables.

But StackOverflow is not a platform for teaching such elementary things, I'm sorry.

Upvotes: 2

Related Questions