微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Randint无法正常工作,我也不知道为什么

如何解决Randint无法正常工作,我也不知道为什么

import turtle#importer turtle pour pouvoir l'utiliser
import random
from random import random #importer random pour pouvoir laisser le choix à l'ordinateur. Le choix sera aléatoire.
 #pour pouvoir utiliser screen turtle,demanderdes questions à l'utilisateur
from turtle import Screen,Turtle
#from random import random 

window = Screen()

bgcolor = None

while bgcolor is None:
    bgcolor = window.textinput("Choose a background color between black,red or yellow","Color")
window.bgcolor(bgcolor)

color = None
while color is None:
    color=window.textinput("quelle couleur voulez vous que les bords soit?","couleur:")
    turtle.pencolor(color)

z=window.textinput("nommez votre fichier(vous ne pouvez qu'utiliser des lettres",'name:')
window.title(z)

turtle.setup(width=0.8,height=0.8)#agrandit le screen
turtle.screensize(10000,10000)#pour que l'utilisateur puisse scrooll

turtle.screensize(canvwidth=400,canvheight=400)

#x=int(window.textinput("choisissez les coordonnées où commencer.","x:"))
#y=int(window.textinput("choisissez les coordonnées où commencer.","y:"))
s=window.textinput("choisissez l'épaisseur de l'étoilee","epaisseur")
nb=int(window.textinput("combien d'étoile","nb:"))
p=0

turtle.pensize(s)
turtle.hideturtle()           #make the turtle invisible
turtle.penup()                #don't draw when turtle moves
turtle.goto(0,0)              #move the turtle to a location xy
turtle.showturtle()           #make the turtle visible
turtle.pendown()              #prepare to draw
turtle.pencolor(color)        #Set pen color = user selected foreground color
turtle.screensize(10000,10000)

#turtle.pencolor(bgcolor)
while p<nb:
    turtle.right(60)
    for sides in range(6):
        turtle.color(random(),random(),random()) #rbg
        turtle.pencolor(color)
        turtle.begin_fill()
        turtle.forward(50)

        turtle.left(120)
        turtle.forward(50)
        turtle.left(120)
        turtle.forward(50)
        turtle.right(180)
        turtle.end_fill()
        turtle.forward(50)
        
    turtle.up()
    turtle.right(random.randint(1,360))#met que ce soit un random chiffre ici
    turtle.forward(random.randint(100,400))#randint
    turtle.down()
             
    p+=1

我的编码方式有问题吗?我的问题是在此之后,当我尝试像这样使用randint时:

turtle.forward(random.randint(100,400))

它给了我这个错误

回溯(最近通话最近): 文件“ /Users/ritamansour/Desktop/tyfkgjhv.py”,第62行,在 turtle.right(random.randint(1,360))#met que ce soit un random chiffre ici AttributeError:“ builtin_function_or_method”对象没有属性“ randint”

我不明白问题出在哪里,因为randint与随机导入无关。如果您需要查看完整的代码,请告诉我。谢谢。

解决方法

导入模块时,将模块对象绑定到模块名称空间中的某个名称。

import random

random模块绑定到“随机”。

from random import random

在random模块中获取“ random”功能对象,并将其重新绑定到您的“ random”变量。这将丢弃您用import random创建的引用,现在您不能再使用“随机”来引用该模块了。

解决方案是删除from random import random,然后在您要呼叫random()的任何地方获取随机数,而改为random.random()

,

太多代码=)。但是感谢您提供足够的上下文!

问题是您将random作为模块导入。

import random  # "random" is the module

然后在下一行中,从具有相同名称的模块中随机导入函数。

from random import random  # "random" is now random.random

因此您的随机函数将起作用,但是

random()  # .XXXXX

此函数破坏了您的模块,您无法使用random.foobar从其访问函数,因为它是一个函数。

random.foobar()  # AttributeError,function random.random doesn't have .foobar

导入两种方式通常是不好的做法,只需执行以下一种方式

import random
 ...
random.random()
random.randint()

from random import random,randint
random()
randint()
,

不是从随机变量中随机导入函数,而是将其作为库导入一次。

import random

print(random.randint(1,360))

所以你想要的是这个

turtle.right(random.randint(1,360))
turtle.forward(random.randint(100,400))

然后更早地以这种方式对随机函数进行不同的调用

turtle.color(random.random(),random.random(),random.random())
,

您导入了随机变量,但您将随机变量与随机变量绑定在一起(来自随机导入随机变量) 这是错误的方式……

您只能导入随机

OR

您可以使用(从随机导入*)

您可以同时做...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。