python-extensions - 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
解决方案
可能,您没有导入随机模块。将此添加到脚本的顶部:
import random
推荐阅读
- react-native - 如何检查whatsapp贴纸是否已添加或未添加到本机反应中?
- python - 如何有效地连接由 for 循环生成的数千个 pandas 数据帧?
- jquery - Overriding the border for required inputs with jquery
- javascript - 多色色轮跟边问题
- database - 数据迁移 ElasticSearch 5.6.16 到 7.2.0
- azure - 将变量分配给 Azure 版本的 Shell 内联脚本
- r - 基于其他值存在的新列
- c# - SQLite3 版本 3.34 - 回调函数
- acquia - 如何在 Windows 机器上安装 Acquai CLI
- dataframe - Julia Dataframe - 并行连接 DataFrame 操作?