Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Random Python-AttributeError:';int';对象没有属性';randint';_Random_Python 3.x_Attributeerror - Fatal编程技术网

Random Python-AttributeError:';int';对象没有属性';randint';

Random Python-AttributeError:';int';对象没有属性';randint';,random,python-3.x,attributeerror,Random,Python 3.x,Attributeerror,作为python课程的一部分,我正在做的任务之一是生成一个介于1到10之间的随机数100000次,并计算每个数字出现的次数。以下是我为此任务编写的代码: import random one = 0 two = 0 three = 0 four = 0 five = 0 six = 0 seven = 0 eight = 0 nine = 0 ten = 0 count = 0 while

作为python课程的一部分,我正在做的任务之一是生成一个介于1到10之间的随机数100000次,并计算每个数字出现的次数。以下是我为此任务编写的代码:

    import random

    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    seven = 0
    eight = 0
    nine = 0
    ten = 0
    count = 0

    while count < 100000:
        random = random.randint(1, 10)

        if random == 1:
            one += 1
        elif random == 2:
            two += 1
        elif random == 3:
            three += 1
        elif random == 4:
            four += 1
        elif random == 5:
            five += 1
        elif random == 6:
            six += 1
        elif random == 7:
            seven += 1
        elif random == 8:
            eight += 1
        elif random == 9:
            nine += 1
        else:
            ten += 1

    count += 1

    print("1 occured " + str(one) + " times")
    print("2 occured " + str(two) + " times")
    print("3 occured " + str(three) + " times")
    print("4 occured " + str(four) + " times")
    print("5 occured " + str(five) + " times")
    print("6 occured " + str(six) + " times")
    print("7 occured " + str(seven) + " times")
    print("8 occured " + str(eight) + " times")
    print("9 occured " + str(nine) + " times")
    print("10 occured " + str(ten) + " times")
随机导入
一=0
二=0
三=0
四=0
五=0
六=0
七=0
八=0
九=0
十=0
计数=0
当计数小于100000时:
random=random.randint(1,10)
如果random==1:
一+=1
elif random==2:
二+=1
elif random==3:
三+=1
elif random==4:
四+=1
elif random==5:
五+=1
elif random==6:
六+=1
elif random==7:
七+=1
elif random==8:
八+=1
elif random==9:
九+=1
其他:
十+=1
计数+=1
打印(“1次出现”+str(一次)+“次”)
打印(“2次发生”+str(两次)+“次”)
打印(“3次出现”+str(三次)+“次”)
打印(“4次出现”+str(四次)+“次”)
打印(“发生5次”+str(五次)+“次”)
打印(“发生6次”+str(六次)+“次”)
打印(“发生7次”+str(七次)+“次”)
打印(“发生8次”+str(8次)+“次”)
打印(“9次出现”+str(九次)+“次”)
打印(“10次出现”+str(十次)+“次”)
然而,我得到一个属性错误说:

    Traceback (most recent call last):
      File "J:/Python/Extension Task - Random Numbers.py", line 19, in <module>
        random = random.randint(1, 10)
    AttributeError: 'int' object has no attribute 'randint'
回溯(最近一次呼叫最后一次):
文件“J:/Python/Extension Task-Random Numbers.py”,第19行,在
random=random.randint(1,10)
AttributeError:“int”对象没有属性“randint”

我已尝试更改标题,使其不包含random一词,但仍然不起作用。我花了很长时间来寻找解决方案,但毫无效果。

您已将其中一个变量命名为random,该变量隐藏了您尝试使用的模块的名称:

random = random.randint(1, 10)

在这一行之后,
random
是您的随机数,而不是
random
模块。请为该变量使用其他名称

谢谢你,我刚刚意识到这一点,并为我的无知和愚蠢道歉:PI由于调用我的文件random.py而犯了同样的错误