模块'random'没有属性'randomint'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模块'random'没有属性'randomint'相关的知识,希望对你有一定的参考价值。

我在尝试解决此问题时已阅读其他文章。他们似乎都说该文件名为random.py,但我的文件名为test.py

def getAnswer(answerNumber):
   if answerNumber == 1:
       return 'It is certain'
   elif answerNumber == 2:
       return 'It is decidedly so'
   elif answerNumber == 3:
       return 'Yes'
   elif answerNumber == 4:
       return 'Reply hazy try again'
   elif answerNumber == 5:
       return 'Ask again Later'
r = random.randomint(1,5)
fortune = getAnswer(r)
pint(fortune) ```
答案

该函数是randint而不是randomint:

import random
def getAnswer(answerNumber):
   if answerNumber == 1:
       return 'It is certain'
   elif answerNumber == 2:
       return 'It is decidedly so'
   elif answerNumber == 3:
       return 'Yes'
   elif answerNumber == 4:
       return 'Reply hazy try again'
   elif answerNumber == 5:
       return 'Ask again Later'
r = random.randint(1,5)
fortune = getAnswer(r)
pint(fortune) 

以上是关于模块'random'没有属性'randomint'的主要内容,如果未能解决你的问题,请参考以下文章

Python常用模块 之 random模块——实现人和电脑猜拳

Python常用模块 之 random模块——实现人和电脑猜拳

python3在pycharm中为啥导入random模块不能用? TypeError: 'module' object is not callable

python random 模块及验证码功能

AttributeError:'NoneType'对象没有属性'fit_generator'

python3在pycharm中为什么导入random模块不能用? TypeError: 'module' object is not callable