Home > database >  'builtin_function_or_method' object has no attribute 'choice'
'builtin_function_or_method' object has no attribute 'choice'

Time:12-15

from random import random ,choices
from turtle import Turtle ,Screen
p=Turtle()
p.color("red")
n=(1,2,3,4)
for f in range(0,50, 1):
    n=["1","2","3","4"]
    c=random.choice(n)
    if c==1:
        p.forward(20)
        if c==2:
            p.backward(20)
            if c==3:
                p.up
                p.forward(20)
                if c==4:
                    p.down
                    p.forward

random.choice is showing following error- AttributeError: 'builtin_function_or_method' object has no attribute 'choice' idk why?

CodePudding user response:

choice (like random and choices) is a funtion defined by the random module, not a method of the random function defined by the same module.

from random import choice, random, choices

CodePudding user response:

replace line 7 and 8

n=["1","2","3","4"]
c=random.choice(n)

with

c=randint(1, 4)

You will also need to add randint to your imports:

from random import random, randint

  • Related