【问题标题】:python random function not working , I am very new at pythonpython随机函数不起作用,我对python很陌生
【发布时间】:2018-10-10 06:26:26
【问题描述】:

我在 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


    【解决方案1】:

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

    import random 
    

    【讨论】:

    • 感谢它做得很好。真的很感激。
    猜你喜欢
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 2016-02-08
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多