简体   繁体   中英

getting random number from a prime list in python

guys i am totally new to programming in python so i need your help please ... i want to generate a prime number from a prime list i have created ... here is my code

list = []
for i in range(2,15):
primeflag=True
for num in list:
    if(i%num==0):
        primeflag=False
if(primeflag):
    list.append(i)
    a = random.choice(list)
print list , a

AttributeError: 'function' object has no attribute 'choice'

it gives me an error so any help ?? thanks in advance

使用import random代替from random import random

In addition to @zero323's answer, if you don't need more than choice function, you can do this instead:

from random import choice

And use it straight away:

a = choice(list)

Also your program might not do what you wanted. Currently, if you find 5 in your list, you will add a new 5 to the list.

Tips: Never use list as a variable name! It will override the built-in function with the same name, list .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM