比较范围内的值

2024-04-20 14:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个变量“wins”和“clicks”,每个变量的值从1100K。你知道吗

当“wins”是150的倍数时,以及当“clicks”是0或1时,需要标记吗?你知道吗

要求:迭代地增加两个值

 if 150<=int(wins)<=300 and 0<=int(clicks)<=1:

Tags: and标记ifint倍数winsclicks
1条回答
网友
1楼 · 发布于 2024-04-20 14:24:08

Need to flag when "wins are in multiples of 150" and when "clicks are 0 OR 1"

试试看

flag = (clicks in (0, 1) and (wins % 150) == 0)
  • clicks in (0, 1):表示单击次数为0或1
  • (wins % 150) == 0:意思是wins % 150余数为零,所以win可以被150整除。你知道吗

检查以下内容:

>>> clicks, wins = 0, 150 * 7
>>> flag = (clicks in (0, 1) and (wins % 150) == 0)
>>> flag
True
>>> clicks, wins = 2, 150 * 7
>>> flag = (clicks in (0, 1) and (wins % 150) == 0)
>>> flag
False
>>> 

注意:如果'clicks'和'winds'是字符串,那么您需要使用typecaseint(clicks)int(wins)。在我的回答中,风和点击都是int


编辑:我试图理解你的评论和问题。以上答案可能对您有所帮助:

comment-1: Data type is int for wins and clicks:

如果winsclicksint值,则不需要使用typecase。只要照我上面回答的做就行了。你知道吗

comment-2: I want to simply retrieve the records with say 150wins and clicks = 0:

实现这个逻辑非常简单:

if winds == 150 and clicks == 0:
  # code to retrieve record  

最后:

comment-3: Increment wins counter in multiples for 150 (should retrieve records when wins is in between 150-300 and clicks = 0)
4) While incrementing wins counter I also need to increment clicks counter to retrieve records

e.g. when wins=[300-450] and clicks=1 retreive,
wins=[300-450] and clicks = 2 skip

很难理解!我仍然相信你需要这样的东西:

# `num` until you wants to execute 
for _ in range(0, num):
  if clicks in (0, 1) and (wins % 150) == 0:
     # code to retrieve record
  wins += 150       

我不知道为什么你要增加点击次数,如果你只想检索点击次数的记录值0,1。你知道吗

相关问题 更多 >