AttributeError:' int'对象没有属性' randint'

时间:2017-10-27 16:45:12

标签: python module attributes

问题在于给出一个新的随机数供用户猜测。它显示错误:

  

AttributeError:' int'对象没有属性' randint'

我的代码就在这里:

import random
print "Try to find the number I have on my mind in once !! " 
print "Everytime you are wrong the number will change !! " 
guess = 0 
energy = True 
while energy == True:
    random = random.randint(1,100)
    guess = input("What's the number ?? : ")
    if guess != random:
        print "Unfortunately you didn't find it  .. "
if guess == random:
        print "Unbelievable ! You found it .. ! " 
        energy = False

2 个答案:

答案 0 :(得分:0)

malloc,而不是randit。这就是python无法找到它的原因。

更新: 你在做 randint 那就是你用随机变量覆盖导入的随机数。将其命名为python关键字/模块/函数名称以外的名称。 例如:random = random.randit(1,100)

答案 1 :(得分:0)

请确保您的文件名不是random.py,因为python会将其解释为模块。

修改

将名为random的变量更改为其他内容。像这样,

import random
print ("Try to find the number I have on my mind in once !! ")
print ("Everytime you are wrong the number will change !! ")
guess = 0 
energy = True 
while energy == True:
    number = random.randint(1,100)
    guess = input("What's the number ?? : ")
    if guess != number:
        print ("Unfortunately you didn't find it  .. ")
if guess == number:
        print ("Unbelievable ! You found it .. ! ") 
        energy = False