我认为它是Python 3.x“未定义名称“ play”的吗?

时间:2018-08-31 22:20:35

标签: python-3.x

完整错误:第10行,在     颜色= play() NameError:名称“ play”未定义

在这里的任何地方,我似乎都找不到找到此问题的原因。我正在尝试将返回的字符串分配给变量color,但是它拒绝识别函数“ play”。

编辑:没关系,我是傻瓜,还没确定玩

import random
Funds = 10
Bet = "Red"
betsequence = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
counter = -1
totalcount = 0

while(Funds > 0):
    counter = counter + 1
    colour = play()
    if colour == Bet:
        Funds = Funds+(betsequence[counter]*2)
        counter = -1
    else:
        Funds = Funds-betsequence[counter]
    print(colour)
    totalcount = totalcount

def play():
    random.seed(a=None, version=2)
    rannum = random.uniform(0,1)
    result = rannum*14
    if (result > 1) and (result < 8):
        return "Red"
    elif result < 1:
        return "Green"
    else:
        return "Black"

1 个答案:

答案 0 :(得分:1)

您需要先定义名称,然后再使用。对于您而言,将play的定义移到while循环之前将解决此问题。