兰丁不工作,我不知道为什么

2024-06-26 14:00:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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时:

乌龟。前进(随机。随机(100400))

它给了我这个错误:

回溯(最近一次呼叫最后一次): 文件“/Users/ritamansour/Desktop/tyfkgjhv.py”,第62行,在 乌龟。右(random.randint(1360))#遇到了一只乌龟 AttributeError:“内置函数”或“方法”对象没有属性“randint”

我不明白问题出在哪里,因为randint是import random的一部分。如果您需要查看完整的代码,请告诉我。多谢各位


Tags: fromimportnonerandomtextinputwindowcolorforward
3条回答

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

import random

random模块绑定到“random”

from random import random

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

解决方案是删除from random import random,然后在调用random()获取随机数的任何地方,改为执行random.random()

导入random,但将random与random like绑定(来自random import random) 这样做是错误的

您只能导入随机数据

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

你可以同时做这两件事

与其从导入函数的random中导入random,不如将其作为库导入一次

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())

相关问题 更多 >