【问题标题】:Why do I get an error "name 'play' is not defined" when I think it is?为什么我认为是“未定义名称'play'”的错误?
【发布时间】:2018-08-31 22:20:34
【问题描述】:

完全错误:

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

我似乎无法在此处的任何地方找到此问题的原因。我正在尝试将返回的字符串分配给变量颜色,但它拒绝识别函数“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"

【问题讨论】:

    标签: python python-3.x nameerror


    【解决方案1】:

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

    【讨论】:

    • 谢谢,在发帖几秒钟后就意识到了这一点。很困惑,因为这是我通常布置 C# 代码的方式,而这实际上是我用 python 编写的第一件事。抱歉浪费了时间!
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    相关资源
    最近更新 更多