如何修复此代码的python错误,我正在使用jupyter笔记本

时间:2019-04-17 16:26:27

标签: python-3.x

我正在编写一个新的python代码,该代码由Strategies类和一个称为play game的函数组成。 这是本机python代码。

%matplotlib inline
from random import randint
from enum import Enum
import matplotlib.pyplot as plt

class Strategie(Enum):
    changer = 1
    garder = 0

def play_game(stratergie):
    portes = [0,1,2]
    bonnes_portes = random.randint(0,2)
    premier_choix = randint(0,2)
    portes.remove(premier_choix)

    if premier_choix == bonnes_portes :
        portes.remove(portes.randint(0,1))
    else:
        portes = [bonnes_portes]

    deuxieme_choix = 0

    if (strategie == Strategie.garder):
        deuxieme_choix = premier_choix 
    else: 
        deuxieme_choix = portes[0]

    return deuxieme_choix == bonnes_portes

play_game(Strategie.changer)

当我编译该程序时,编译器会向我显示此错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-a9fed9101c49> in <module>()
----> 1 play_game(Strategie.changer)

NameError: name 'Strategie' is not defined

1 个答案:

答案 0 :(得分:0)

谢谢您的帮助。

现在,我的代码可以正常工作了,并且调用Randint函数时出错,我必须从random.randint中删除random,这是我代码的最新版本。

%matplotlib inline
from random import randint, seed
from enum import Enum
import matplotlib.pyplot as plt

class Strategie(Enum):
    changer = 1
    garder = 0

def play_game(strategie):
    portes = [0,1,2]
    bonnes_portes = randint(0,2)
    premier_choix = randint(0,2)
    portes.remove(premier_choix)

    if premier_choix == bonnes_portes :
        portes.remove(portes.randint(0,1))
    else:
        portes = [bonnes_portes]

    deuxieme_choix = 0

    if (strategie == Strategie.garder):
        deuxieme_choix = premier_choix 
    else: 
        deuxieme_choix = portes[0]

    return deuxieme_choix == bonnes_portes 

play_game(Strategie.changer)