我一直得到属性错误randint

时间:2017-02-10 08:20:42

标签: python python-3.x

每次我运行我的代码时我都会得到属性错误而int没有randint,但是当我在网上查看如何做随机时,就是它告诉我要做的,请帮助。

def gorandom():
        if random.randint(1,8) == 1:
            turtle.goto(-250,250)
        elif random.randint(1,8) == 2:
            turtle.goto(0,250)
        elif random.randint(1,8) == 3:
            turtle.goto(250,250)
        elif random.randint(1,8) == 4:
            turtle.goto(250,0)
        elif random.randint(1,8) == 5:
            turtle.goto(250,-250)
        elif random.randint(1,8) == 6:
            turtle.goto(0,-250)
        elif random.randint(1,8) == 7:
            turtle.goto(-250,-250)
        else:
            turtle.goto(-250,0)

3 个答案:

答案 0 :(得分:0)

您缺少导入。请添加

import random

到文件的顶部。

答案 1 :(得分:0)

试试这个:

import random

def gorandom():
    if random.randint(1,8) == 1:
        turtle.goto(-250,250)
    elif random.randint(1,8) == 2:
        turtle.goto(0,250)
    elif random.randint(1,8) == 3:
        turtle.goto(250,250)
    elif random.randint(1,8) == 4:
        turtle.goto(250,0)
    elif random.randint(1,8) == 5:
        turtle.goto(250,-250)
    elif random.randint(1,8) == 6:
        turtle.goto(0,-250)
    elif random.randint(1,8) == 7:
        turtle.goto(-250,-250)
    else:
        turtle.goto(-250,0)

确保你的goto(x,y)函数有效;)

答案 2 :(得分:0)

找到解决方案,谢谢你的帮助

from random import randint

def gorandom():
        if randint(1,8) == 1:
           turtle.goto(-250,250)
        elif randint(1,8) == 2:
            turtle.goto(0,250)
        elif randint(1,8) == 3:
            turtle.goto(250,250)
        elif randint(1,8) == 4:
            turtle.goto(250,0)
        elif randint(1,8) == 5:
            turtle.goto(250,-250)
        elif randint(1,8) == 6:
            turtle.goto(0,-250)
        elif randint(1,8) == 7:
            turtle.goto(-250,-250)
        else:
            turtle.goto(-250,0)