"变量未在函数中定义"错误

时间:2015-09-15 23:12:45

标签: python

我必须用Python制作一个骰子游戏,但是我已经挂断了让函数正常工作。

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()

当我运行这个程序时,它正常工作,直到它到达函数rollSelected()并给我一个错误。该错误表明lRollAgain函数中未定义rollSelected

有人可能会解释为什么lRollAgain函数中没有定义rollSelected?因为它在whichRoll函数中。我想我只是不知道它在问什么。

2 个答案:

答案 0 :(得分:2)

您正在使用您的变量,就好像它们是全局一样。虽然你的语法使他们本地。 (我建议查阅这些术语。)因此,它们只存在于其狭窄的范围内(例如它们所使用的功能)。在一个函数中设置变量并在另一个函数中使用它只适用于全局变量。

快速解决方案是将所有变量设为全局变量:

lRollAgain = None

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

更好的方法是了解范围,本地,函数参数和返回值。然后你应该通过参数将 values 从函数传递给函数并返回值,而不是通过全局变量这样做。

但是StackOverflow不是教这么基本的东西的平台,对不起。

答案 1 :(得分:0)

它说因为IRollAgain只是变量的名称。你在whichRoll函数中声明它,这意味着它只表示whichRoll函数中的某些东西。由于rollSelected函数不在whichRoll函数中,因此IRollAgain在rollSelected中没有任何意义。您必须通过传递IRollAgain作为rollSelected函数中的参数,再次将其声明为您想要的内容。即

rollSelected(thisRoll)

编辑:

我也认为你的意思是你的whichRoll功能看起来更像这样:

def whichRoll():

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

这样,您将返回您输入的值。我还建议进行一些检查以确保输入正确