English 中文(简体)
• 与其他零用模块操作合作?
原标题:Working with a remainder of Zero with modulo operation?
  • 时间:2011-10-01 22:28:11
  •  标签:
  • python

在模块操作员的剩余零件工作方面,我遇到一些困难。

基本上,这是我法典的一条线。

fday= (day+24) % 30 

我只想增加24天,用其余时间。 然而,如果一天用户进入6个用户,结果就等于我不想要的零。

如果进入了6个行动,我如何能够使行动返回6个? 是否有更好的办法这样做?

<>Update:

I m试图采用以前确定的变量(day),并将这一数值增加24天。

然而,如果用户进入该月的第25天,那么我就增加了24天,但每月有49天。

因此,我试图利用单元行动给我剩余的东西,因为它在一个月里工作了30天。

另一个例子:

如果是5个投入,则为5+24,为29个,然后是29%30 = 29。 因此,5个工厂为我试图做的工作(这只是增加价值24天,使产出保持在30天以下(因为每月只有30天时间用于我做的事情)。

问题回答

You say in the comments that for 5 you want 29, for 6 you want 6, and for 7 you want 1. That doesn t make sense. The correct value for 6 is 30 not 6 or 0 when you re calculating a day modulo 30.

In math, division by 30 gives a remainder between 0 and 29 -- that s what modulo 30 is. What you want is a number between 1 and 30.

因此,你需要花一天时间:subtract 1。 (到029而不是130>之间,再加24,再加< <>30>, 即<>addcode>1 (在130之间回过一天)。

result = ((day - 1 + 24) % 30) + 1

That way you ll always get the correct number between 1 and 30 and you don t have to think of 6 as a special case.

Consider using the functionality in the datetime module instead of trying to reinvent it:

>>> from datetime import date, timedelta
>>> for month in (4, 5):
...     for day in (5, 6, 7, 8):
...         start = date(2011, month, day)
...         later = start + timedelta(days=24)
...         print str(start), str(later)
...
2011-04-05 2011-04-29
2011-04-06 2011-04-30
2011-04-07 2011-05-01
2011-04-08 2011-05-02
2011-05-05 2011-05-29
2011-05-06 2011-05-30
2011-05-07 2011-05-31
2011-05-08 2011-06-01




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签