[Python] If logic not obtaining desired result [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have code which gives out of product profit/loss based on buyer conditions. The code is giving me following results.

    a_t_p= 0
    a_t_p= 0
    a_t_p= 0
    a_t_p= 0
    a_t_p= 541.4000000000015
    a_t_p= 541.4000000000015
    a_t_p= 546.9500000000044
    a_t_p= 546.9500000000044
    a_t_p= 587.8500000000058
    a_t_p= 587.8500000000058
    a_t_p= 618.2000000000044
    a_t_p= 618.2000000000044
    a_t_p= 631.8500000000058
    a_t_p= 631.8500000000058


    where as i am expecting following desired results.

    a_t_p = 0
    a_t_p = 0
    a_t_p = 0
    a_t_p = 28.0500000000029
    a_t_p = 569.450000000004
    a_t_p = 564.150000000001
    a_t_p = 569.700000000004
    a_t_p = 417.050000000003
    a_t_p = 457.950000000004
    a_t_p = 484.050000000003
    a_t_p = 514.400000000001
    a_t_p = 486.450000000004
    a_t_p = 500.100000000006
    a_t_p = 551.700000000004


    so I guess the problem is the program is not capturing the actual_pl when z=7 in the second loop. Itried multiple changes but not sure why is this mistake is happening.

    My code is as follows.

    actual_purchase_running= ""
    max_loss_on_product = -1500
    actual_pl = 0
    first_purchase = 0
    first_buyer = 0
    first_buyer_prefrence = 0
    run_pl = 0
    normal_pl = 0
    position_of_buyer = ""
    first_purchase_running = ""
    first_purchase_pl = 0


    def get_next_z():
    for val in z_list:
    yield val


    def get_next_TV_price():
    for val in TV_price_list:
    yield val


    z_list = [4, 7, 4, 7, 4, 7, 4, 7, 4, 7, 4, 7, 4]
    TV_price_list = [50892.3, 50941.95, 50970, 51511.4, 51506.1, 51511.65, 51359, 51399.9, 51426, 51456.35, 51428.4, 51442.05,
    51493.65]

    get_z = get_next_z()
    get_TV_price = get_next_TV_price()

    # when sec =0
    while True:
    print('-a_t_p= ', actual_pl)

    z = next(get_z)
    TV_price = next(get_TV_price)

    if z == 4 or z == 7:
    if first_purchase_running == "done":
    pass
    else:
    if first_purchase_running == "with 4":
    if z == 4:
    continue
    elif z == 7:
    # reversal here

    TV_price_s = TV_price
    first_purchase_pl = TV_price_s + TV_price_b
    if first_purchase_pl < 0:
    first_buyer_prefrence = "bad"
    run_pl = run_pl + first_purchase_pl
    first_purchase_running = "done"
    normal_purchase_running = "with 7"
    # actual purchase block
    if first_buyer == 4 and first_buyer_prefrence == "good":
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = "with 4"
    position_of_buyer = "happy"
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 4 and first_buyer_prefrence == 'bad':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 7 and first_buyer_prefrence == 'good':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    else:
    continue
    elif first_purchase_pl > 0:
    first_buyer_prefrence = "good"
    run_pl = run_pl + first_purchase_pl
    first_purchase_running = "done"
    normal_purchase_running = "with 7"
    # actual_purchase_block
    if first_buyer == 4 and first_buyer_prefrence == "good":
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = "with 4"
    position_of_buyer = "happy"
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 4 and first_buyer_prefrence == 'bad':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 7 and first_buyer_prefrence == 'good':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    else:
    continue

    else:
    continue
    elif first_purchase_running == "with 7":
    if z == 7:
    continue
    elif z == 4:
    # reversal here

    TV_price_b = -TV_price
    first_purchase_pl = TV_price_s + TV_price_b
    if first_purchase_pl < 0:
    first_buyer_prefrence == "bad"
    run_pl = run_pl + first_purchase_pl
    first_purchase_running = "done"
    normal_purchase_running = "with 4"
    # actual_purchase_block
    if first_buyer == 4 and first_buyer_prefrence == "good":
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = "with 4"
    position_of_buyer = "happy"
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 4 and first_buyer_prefrence == 'bad':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 7 and first_buyer_prefrence == 'good':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    else:
    continue
    continue
    elif first_purchase_pl > 0:
    first_buyer_prefrence = "good"
    run_pl = run_pl + first_purchase_pl
    first_purchase_running = "done"
    normal_purchase_running = "with 4"
    # actual_purchase_block
    if first_buyer == 4 and first_buyer_prefrence == "good":
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = "with 4"
    position_of_buyer = "happy"
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 4 and first_buyer_prefrence == 'bad':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif first_buyer == 7 and first_buyer_prefrence == 'good':
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif z == 7:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    else:
    continue
    continue
    else:
    continue
    else:
    # need to make 1st purchase
    if z == 4:
    # happy here

    TV_price_b = -TV_price
    first_purchase_running = "with 4"
    first_buyer = 4
    continue
    elif z == 7:
    # sad here

    TV_price_s = TV_price
    first_purchase_running = "with 7"
    first_buyer = 7
    continue
    # run pl
    if normal_purchase_running == "with 4":
    if z == 4:
    pass
    elif z == 7:
    # reversal here

    TV_price_s = TV_price
    normal_pl = TV_price_s + TV_price_b
    run_pl = run_pl + normal_pl
    normal_purchase_running = "with 7"
    else:
    pass
    elif normal_purchase_running == "with 7":
    if z == 7:
    pass
    elif z == 4:
    # reversal here

    TV_price_b = -TV_price
    normal_pl = TV_price_s + TV_price_b
    run_pl = run_pl + normal_pl
    normal_purchase_running = "with 4"

    # actual purchase run
    # 4 pos
    if first_buyer == 4 and first_buyer_prefrence == 'good':
    if actual_purchase_running == 'with 4':
    if z == 4:
    if position_of_buyer == 'happy':

    TV_price_s = TV_price
    sl = TV_price_s + TV_price_b + actual_pl
    if sl < max_loss_on_product:
    print('break')
    continue
    # break
    elif sl >= max_loss_on_product:
    continue
    elif position_of_buyer == 'sad':

    TV_price_b = -TV_price
    sl = TV_price_s + TV_price_b + actual_pl
    if sl < max_loss_on_product:
    print('break')
    # break

    elif sl >= max_loss_on_product:
    continue
    elif z == 7:
    # reversal
    if position_of_buyer == 'happy':

    TV_price_s = TV_price
    actual_pl = actual_pl + TV_price_s + TV_price_b
    if actual_pl < max_loss_on_product:
    print('break')
    # break
    elif actual_pl >= max_loss_on_product:
    # take new purchase
    if run_pl < 0:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    elif run_pl >= 0:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif position_of_buyer == 'sad':

    TV_price_b = -TV_price
    actual_pl = actual_pl + TV_price_s + TV_price_b
    if actual_pl < max_loss_on_product:
    print('break')
    # break
    elif actual_pl > max_loss_on_product:
    # take new purchase
    if run_pl < 0:
    # sad here

    TV_price_sf = TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'sad'
    continue
    elif run_pl >= 0:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 7'
    position_of_buyer = 'happy'
    continue
    elif actual_purchase_running == 'with 7':
    if z == 7:
    if position_of_buyer == 'happy':

    TV_price_s = TV_price
    sl = TV_price_s + TV_price_b + actual_pl
    if sl < max_loss_on_product:
    print('break')
    # break
    elif sl > max_loss_on_product:
    continue
    elif position_of_buyer == 'sad':

    TV_price_b = -TV_price
    sl = actual_pl + TV_price_s + TV_price_b
    if sl < max_loss_on_product:
    print('break')
    # break
    elif sl > max_loss_on_product:
    pass

    elif z == 4:
    # reversal
    if position_of_buyer == 'happy':

    TV_price_s = TV_price
    actual_pl = TV_price_s + TV_price_b + actual_pl
    if actual_pl < max_loss_on_product:
    print('break')
    # break
    elif actual_pl >= max_loss_on_product:
    # take new purchase
    if run_pl < 0:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'sad'
    continue
    elif run_pl >= 0:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue
    elif position_of_buyer == 'sad':

    TV_price_b = -TV_price
    actual_pl = TV_price_s + TV_price_b + actual_pl
    if actual_pl < max_loss_on_product:
    print('break')
    # break
    elif actual_pl >= max_loss_on_product:
    # take new purchase
    if run_pl < 0:
    # sad here

    TV_price_s = TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'sad'
    continue
    elif run_pl >= 0:
    # happy here

    TV_price_b = -TV_price
    actual_purchase_running = 'with 4'
    position_of_buyer = 'happy'
    continue


    else:
    if actual_purchase_running == "with 4" or actual_purchase_running == "with 7":

    if position_of_buyer == "happy":
    TV_price_s = TV_price
    sl = TV_price_s + TV_price_b
    if actual_pl + sl < max_loss_on_product:
    print('break')
    # break
    else:
    continue
    elif position_of_buyer == "sad":
    TV_price_b = -TV_price
    sl = TV_price_s + TV_price_b
    if actual_pl + sl < max_loss_on_product:
    print('break')
    # break
    else:
    continue

    Continue reading...

Compartilhe esta Página