为什么会出现错误“名称“播放"未定义"?我认为是什么时候? [英] Why do I get an error "name 'play' is not defined" when I think it is?

查看:119
本文介绍了为什么会出现错误“名称“播放"未定义"?我认为是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整错误:

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

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

I can't seem to find a reason for this issue anywhere on here. I am trying to assign the returned string to the variable colour but it is refusing to recognise the function "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循环之前将解决此问题.

You need to define the name before it is first used. In your case, moving the definition of play to before the while loop will solve the issue.

这篇关于为什么会出现错误“名称“播放"未定义"?我认为是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆