为什么我认为是错误 "name 'play' is not defined"?

Why do I get an error "name 'play' is not defined" when I think it is?

完整错误:

line 10, in <module>
    colour = play()
NameError: name 'play' is not defined

我似乎无法在这里的任何地方找到这个问题的原因。我正在尝试将返回的字符串分配给变量 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"

您需要在首次使用前定义名称。在您的情况下,将 play 的定义移动到 while 循环之前将解决问题。