Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何使变量在40%的时间内为真_Python_Django_Django Views - Fatal编程技术网

Python:如何使变量在40%的时间内为真

Python:如何使变量在40%的时间内为真,python,django,django-views,Python,Django,Django Views,以下情况的最佳方法是什么 在Django中,我有一个名为“showItem”的变量视图,该变量可以是true或false。我想将showItem设置为true40%,false60%,并提供稍后更改这些赔率的选项 使用Python我应该怎么做 部分意见: def get_context_data(self, **kwargs): context = super(EntryDetail, self).get_context_data(**kwargs) conte

以下情况的最佳方法是什么

在Django中,我有一个名为“
showItem
”的变量视图,该变量可以是
true
false
。我想将showItem设置为true40%,false60%,并提供稍后更改这些赔率的选项

使用Python我应该怎么做

部分意见:

 def get_context_data(self, **kwargs):
        context = super(EntryDetail, self).get_context_data(**kwargs)
        context['showItem'] = (odds?????)
        return context
随机导入
num=random.randint(0,4)
如果num
import random,则上下文['showItem']=True
num=random.randint(0,4)

context['showItem']=如果num则为True以下是另一种方法:

import random

cur_num   = random.random()
threshold = 0.4

# showItem is true 60 % of the time
showItem = cur_num >= threshold

如果您选择以后更改阈值,只需修改
threshold
变量,这比列出的其他方法更简单。

以下是另一种方法:

import random

cur_num   = random.random()
threshold = 0.4

# showItem is true 60 % of the time
showItem = cur_num >= threshold

如果你选择以后更改阈值,你只需修改
threshold
变量,这比列出的其他方法更简单。

试试
random.randrange(100)>40
()。试试
random.randrange(100)>40
()。nice import random是完美的。他说他希望以后改变几率。哦,我没有看到orz,nice import random是完美的,他说他想以后改变赔率。哦,我没有看到orz。