Why do I get this "'builtin_function_or_method' object has no attribute 'choice'" error?
Question:
I want to write a small program that
does the following:
it should draw many small triangles which are lined up and have different colors.
Unfortunately I get the following error message:
'builtin_function_or_method' object has no attribute 'choice'
I have already looked a bit on the internet but I could not fix my error
This is my code:
from turtle import *
from random import *
def triangleDrawer():
fd(20)
rt(120)
fd(20)
rt(120)
fd(20)
rt(120)
fd(20)
colors = ["red","green","blue","orange","purple","pink","yellow"]
def pictureDrawer():
i = 1
jumper = 10
x = -200
y = 200
up()
goto(x, y)
down()
while i<100:
triangleDrawer()
currentColor = random.choice(colors)
color(currentColor)
if(i == jumper):
y = y - 18
up()
goto(x, y)
down()
jumper += 10
i+=1
pictureDrawer()
Answers:
The problem is this import:
from random import *
You import random.random
as random
and try to use it as the module.
You also import random.choice
as choice
this way so its available as choice
.
Either use:
import random
Which will namespace everything under random
as you expect.
Or use choice
instead of random.choice
:
currentColor = choice(colors)
I had the same error; import random as random
did the job.
I want to write a small program that
does the following:
it should draw many small triangles which are lined up and have different colors.
Unfortunately I get the following error message:
'builtin_function_or_method' object has no attribute 'choice'
I have already looked a bit on the internet but I could not fix my error
This is my code:
from turtle import *
from random import *
def triangleDrawer():
fd(20)
rt(120)
fd(20)
rt(120)
fd(20)
rt(120)
fd(20)
colors = ["red","green","blue","orange","purple","pink","yellow"]
def pictureDrawer():
i = 1
jumper = 10
x = -200
y = 200
up()
goto(x, y)
down()
while i<100:
triangleDrawer()
currentColor = random.choice(colors)
color(currentColor)
if(i == jumper):
y = y - 18
up()
goto(x, y)
down()
jumper += 10
i+=1
pictureDrawer()
The problem is this import:
from random import *
You import random.random
as random
and try to use it as the module.
You also import random.choice
as choice
this way so its available as choice
.
Either use:
import random
Which will namespace everything under random
as you expect.
Or use choice
instead of random.choice
:
currentColor = choice(colors)
I had the same error; import random as random
did the job.