如何使用复选框跳过Python中的日期

时间:2019-04-15 19:56:59

标签: python checkbox

我有4个复选框,每个复选框代表一个日期,例如:

Week 1: 01/01/2019  [  ] - checkbox 1
Week 2: 08/01/2019  [  ] - checkbox 2
Week 3: 15/01/2019  [  ] - checkbox 3
Week 4: 22/01/2019  [  ] - checkbox 4
Week To Pay 29/01/2019

我尝试了以下代码

W1 = W1 = timedelta(days=7)
week_one_raw = "01/01/2019"
week_one_raw = datetime.strptime(FIXED_WEEK_1, '%d/%m/%Y')
week_one_refined = week_one_raw
week_two_refined = week_one_raw + W1
week_three_refined = week_two_refined + W1
week_four_refined = week_three_refined + W1
week_to_pay_refined = week_four_refined + W1

if BOX_1 == 1:
    week_one_refined = week_one_raw + W1
    week_two_refined = week_one_refined + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_1 == 0:
    week_one_refined = week_one_raw
    week_two_refined = week_one_raw + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_2 == 1:
    week_one_refined = week_one_refined + W1
    week_two_refined = week_two_refined + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_2 == 0:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined
    week_four_refined = week_four_refined
    week_to_pay_refined = week_four_refined + W1

  • 如果Box 1和2等于零,则输出正确
  • 如果方格1和2等于1,则输出正确
  • 如果Box 1等于1,而Box 2等于0,则输出是正确的
  • 如果Box 1等于0,Box 2等于1,则输出不正确,因为它跳过了week_one_refined返回值,比它应该执行的操作提前一周。

2 个答案:

答案 0 :(得分:0)

if BOX_2 == 1:
    week_one_refined = week_one_refined

代码错误。


#the full code that works


import datetime
from datetime import datetime
from datetime import timedelta

FIXED_WEEK_1 = "15/04/2019" 


W1 = timedelta(days=7)
W2 = timedelta(days=14)
W3 = timedelta(days=21)
W4 = timedelta(days=28)
W5 = timedelta(days=35)

BOX_1 = 0
BOX_2 = 0
BOX_3 = 0
BOX_4 = 0

week_one_raw = datetime.strptime(FIXED_WEEK_1, '%d/%m/%Y')
week_one_refined = week_one_raw
week_two_refined = week_one_raw + W1
week_three_refined = week_two_refined + W1
week_four_refined = week_three_refined + W1
week_to_pay_refined = week_four_refined + W1

if BOX_1 == 1:
    week_one_refined = week_one_raw + W1
    week_two_refined = week_one_refined + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_1 == 0:
    week_one_refined = week_one_raw
    week_two_refined = week_one_raw + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_2 == 1:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined + W1
    week_three_refined = week_two_refined + W1
    week_four_refined = week_three_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_2 == 0:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined
    week_four_refined = week_four_refined
    week_to_pay_refined = week_four_refined + W1

if BOX_3 == 1:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined +W1
    week_four_refined = week_four_refined + W1
    week_to_pay_refined = week_four_refined + W1

if BOX_3 == 0:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined
    week_four_refined = week_four_refined
    week_to_pay_refined = week_four_refined + W1

if BOX_4 == 1:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined
    week_four_refined = week_four_refined +W1
    week_to_pay_refined = week_four_refined +W1

if BOX_4 == 0:
    week_one_refined = week_one_refined
    week_two_refined = week_two_refined
    week_three_refined = week_three_refined
    week_four_refined = week_four_refined
    week_to_pay_refined = week_four_refined + W1

print (week_one_refined)
print (week_two_refined)
print (week_three_refined)
print (week_four_refined)
print (week_to_pay_refined)

答案 1 :(得分:0)

答案实际上似乎复杂得多,但实际上却很简单。

按照流程图操作,使我了解了需要打开的复选框的所有可能组合。最终看起来像一个反向比赛。最后,它使用嵌套的for循环来使其起作用。

对于感兴趣的人,请参见以下代码:

import datetime
from datetime import datetime
from datetime import timedelta

FIXED_WEEK_1 = "16/04/2019" 


W1 = timedelta(days=7)
W2 = timedelta(days=14)
W3 = timedelta(days=21)
W4 = timedelta(days=28)
W5 = timedelta(days=35)
W6 = timedelta(days=42)
W7 = timedelta(days=49)
W8 = timedelta(days=56)

BOX_1 = 1
BOX_2 = 0
BOX_3 = 1
BOX_4 = 1

week_one_raw = datetime.strptime(FIXED_WEEK_1, '%d/%m/%Y')
week_one_refined = week_one_raw
week_two_refined = week_one_raw + W1
week_three_refined = week_two_refined + W1
week_four_refined = week_three_refined + W1
week_to_pay_refined = week_four_refined + W1

if BOX_1 == 0:
    week_one_refined = week_one_raw
    if BOX_2 == 0:
        week_two_refined = week_one_raw + W1
        if BOX_3 == 0:
            week_three_refined = week_one_raw + W2
            if BOX_4 == 0:
                week_four_refined = week_one_raw + W3
                week_to_pay_refined = week_one_raw + W4
            if BOX_4 == 1:
                week_four_refined = week_one_raw +W4
                week_to_pay_refined = week_one_raw +W5
        if BOX_3 == 1:
            week_three_refined = week_one_raw + W3
            if BOX_4 == 0:
                week_three_refined = week_one_raw +W3
                week_four_refined = week_one_raw +W4
                week_to_pay_refined = week_one_raw +W5
            if BOX_4 == 1:
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W5
                week_to_pay_refined = week_one_raw +W6

    if BOX_2 == 1:
        week_two_refined = week_one_raw + W2
        if BOX_3 == 0:
            week_two_refined = week_one_raw +W2
            if BOX_4 == 0:
                week_two_refined = week_one_raw + W2
                week_three_refined = week_one_raw +W3
                week_four_refined = week_one_raw +W4
                week_to_pay_refined = week_one_raw +W5
            if BOX_4 == 1:
                week_two_refined = week_one_raw +W3
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W6
                week_to_pay_refined = week_one_raw +W7
        if BOX_3 == 1:
            week_two_refined = week_one_raw +W3
            if BOX_4 == 0:
                week_two_refined = week_one_raw + W3
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W5
                week_to_pay_refined = week_one_raw +W6
            if BOX_4 == 1:
                week_two_refined = week_one_raw + W4
                week_three_refined = week_one_raw +W5
                week_four_refined = week_one_raw +W6
                week_to_pay_refined = week_one_raw +W7



if BOX_1 == 1:
    week_one_refined = week_one_raw + W1
    if BOX_2 == 0:
        week_one_refined = week_one_raw +W1
        if BOX_3 == 0:
            week_one_refined = week_one_raw +W1
            if BOX_4 ==0:
                week_one_refined = week_one_raw +W1
                week_two_refined = week_one_raw +W2
                week_three_refined = week_one_raw +W3
                week_four_refined = week_one_raw +W4
                week_to_pay_refined = week_one_raw +W5
            if BOX_4 ==1:
                week_one_refined = week_one_raw +W1
                week_two_refined = week_one_raw +W2
                week_three_refined = week_one_raw +W3
                week_four_refined = week_one_raw +W5
                week_to_pay_refined = week_one_raw +W6
        if BOX_3 ==1:
            week_one_refined = week_one_raw +W1
            if BOX_4 == 0:
                week_one_refined = week_one_raw +W1
                week_two_refined = week_one_raw +W2
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W5
                week_to_pay_refined = week_one_raw +W6
            if BOX_4 == 1:
                week_one_refined = week_one_raw +W1
                week_two_refined = week_one_raw +W2
                week_three_refined = week_one_raw +W5
                week_four_refined = week_one_raw +W6
                week_to_pay_refined = week_one_raw +W7

    if BOX_2 == 1:
        week_one_refined = week_one_raw + W2
        if BOX_3 == 0:
            if BOX_4 ==0:
                week_one_refined = week_one_raw +W2
                week_two_refined = week_one_raw +W3
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W5
                week_to_pay_refined = week_one_raw +W6
            if BOX_4 ==1:
                week_one_refined = week_one_raw +W2
                week_two_refined = week_one_raw +W3
                week_three_refined = week_one_raw +W4
                week_four_refined = week_one_raw +W6
                week_to_pay_refined = week_one_raw +W7
        if BOX_3 ==1:
            if BOX_4 ==0:
                week_one_refined = week_one_raw + W3
                week_two_refined = week_one_raw + W4
                week_three_refined = week_one_raw + W5
                week_four_refined = week_one_raw + W6
                week_to_pay_refined = week_one_raw + W7
            if BOX_4 ==1:
                week_one_refined = week_one_raw + W4
                week_two_refined = week_one_raw + W5
                week_three_refined = week_one_raw + W6
                week_four_refined = week_one_raw + W7
                week_to_pay_refined = week_one_raw + W8

为循环中的某些不一致而致歉...就像重复值一样。我花了一段时间才找出正确的方法。