简体   繁体   English

从Python的素数列表中获取随机数

[英]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 伙计们,我是python编程的新手,所以我需要您的帮助...我想从我创建的素数列表中生成素数...这是我的代码

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: 除了@ zero323的答案外,如果您不需要的只是choice功能,还可以执行以下操作:

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. 当前,如果您在列表中找到5 ,则将新的5添加到列表中。

Tips: Never use list as a variable name! 提示:切勿将list用作变量名! It will override the built-in function with the same name, list . 它将覆盖具有相同名称list的内置函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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