python beginner craps:未定义全局名称 - 命名变量

时间:2017-01-07 07:45:25

标签: python function global user-defined-functions

我是Python编程的新手。因此,我知道这个问题对某些人来说是非常基本的。我的问题是python抛出了一个错误全局名称' firstRollValue'在编译时未定义。我已经编写了来自在线教程的小程序,这些程序具有类似的结构,可以正常工作(即从函数调用函数)。我知道有更简洁的方法来编写类似的程序,但我试图衡量我对函数和返回值的理解。我应该如何定义变量,例如firstRollValue,以便该程序运行?

import random

def test_craps_Game():
    firstRoll()
    assessFirstRoll()
    rollForPoint()

def firstRoll():
    firstRollValue = random.choice([1, 2, 3, 4, 5, 6]) + random.choice([1, 2, 3, 4, 5, 6])
    return firstRollValue

def assessFirstRoll():
    if firstRollValue == [2, 3, 12]:
        print 'You rolled ' + firstRollValue + '. ' + '\nYou Lose!'
    elif firstRollValue == [7, 11]:
        print 'You rolled ' + firstRollValue + '. ' + '\nYou Win!'
    else:
        shooterHasPoint = True
        shooterPoint = firstRollValue
        print 'Your point is ' + shooterPoint
    return shooterPoint

def rollForPoint():
    shooterHasPoint = True
    while shooterHasPoint == True:
        lastRollValue = random.choice([1, 2, 3, 4, 5, 6]) + random.choice([1, 2, 3, 4, 5, 6])
        if lastRollValue == shooterPoint:
            print 'You rolled ' + lastRollValue + ' your point, ' + 'You Win!'
            shooterResult = 'Shooter Wins'
            shooterHasPoint = False
        elif lastRollValue == 7:
            print 'You crapped ' + lastRollValue + ' you lose.'
            shooterResult = 'Shooter Loses'
            shooterHasPoint = False
        return shooterResult

0 个答案:

没有答案