【问题标题】:Attribute Error属性错误
【发布时间】:2013-12-05 00:05:18
【问题描述】:

我打开了一个命令提示符,看看为什么这段代码不起作用,它说:

AttributeError: 'module' object has no attribute 'randint'

import random

print('I am thinking of a number between 1 and 50')

number = random.randint(1, 50)
guess = int(input('Can you guess what it is: '))

while guess != number:
    if guess > number:
        print('Lower...')

    else:
        print('higher...')

    guess = int(input('Can you guess what it is: '))


print('Correct!')

input('\n\nEnter.')

【问题讨论】:

  • 您似乎还有另一个名为 random 的模块。如果是这样,您必须将其重命名为不同的名称以避免命名空间问题
  • 也许你已经将你的 python 文件命名为 random.py ?
  • 请给我们完整真实的回溯,而不是平淡无奇的文字。
  • 代码对我来说很好,也许你的文件名是 random.py ?或者你有另一个名为 random 的模块?

标签: python attributeerror


【解决方案1】:

您在当前目录中有一个名为random.py 的脚本(可能是这个脚本)。将其重命名为其他名称,因为它隐藏了标准 import random。

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 2011-05-15
    • 2018-05-01
    • 2014-03-21
    • 2013-07-01
    • 2013-11-03
    • 2011-10-12
    相关资源
    最近更新 更多