首页 > 解决方案 > python随机函数不起作用,我对python很陌生

问题描述

我在 python 中使用了一个随机函数,但它不起作用,我对 python 很陌生。请查看以下代码

  def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['number'] = random.randrange(1, 100)
        return context

其重新调整错误 NameError: name 'random' is not defined

标签: python-extensions

解决方案


可能,您没有导入随机模块。将此添加到脚本的顶部:

import random 

推荐阅读