Python教程

python3 运行行报错 AttributeError: module ‘random‘ has no attribute ‘randint‘

本文主要是介绍python3 运行行报错 AttributeError: module ‘random‘ has no attribute ‘randint‘,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

今天在运行文件 random.py文件时报错了 AttributeError: module ‘random’ has no attribute ‘randint’。

random.py 文件如下

import random


def v_code():
    code = ""
    for i in range(5):
        num = random.randint(0, 9)
        char = chr(random.randint(65, 90))
        add = random.choice([num, char])
        code = code + str(add)
    return code


print(v_code())

报错信息如下:

Traceback (most recent call last):
  File "E:/Gitee/python_study/Python_script/day15_module_lesson/常见的模块/random.py", line 7, in <module>
    import random
  File "E:\Gitee\python_study\Python_script\day15_module_lesson\常见的模块\random.py", line 20, in <module>
    print(v_code())
  File "E:\Gitee\python_study\Python_script\day15_module_lesson\常见的模块\random.py", line 13, in v_code
    num = random.randint(0, 9)
AttributeError: module 'random' has no attribute 'randint'

Process finished with exit code 1

解决方法:
原来是因为命名.py文件为random,和系统的有冲突导致的。
更改了脚本的名称,就可以解决了。

这篇关于python3 运行行报错 AttributeError: module ‘random‘ has no attribute ‘randint‘的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!