我只想在遞回中應用一次條件。我的資料結構如下
stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}
我想從中隨機選擇任何階段并將狀態更改為 True。但是當真實階段的總數為 3 時,我想將 True 階段隨機更改為 False。但只有一次。然后它應該繼續將階段變為 True。當所有 4 個階段都為真時。遞回程序停止。我怎樣才能做到這一點 ?我已經嘗試了以下代碼。但它并不完整。
def process(stages):
all_stages = [stage for stage, status in stages.items() if status == False]
if len(all_stages) !=0:
print(all_stages)
select_ = random.choice(all_stages)
print("\tselected stage: ",select_)
stages[select_] = True
process(stages)
else:
print("Done")
print(stages)
process(stages)
這在不添加額外條件的情況下有效。我已經嘗試過以下一種。但這不起作用
def process(stages):
all_stages = [stage for stage, status in stages.items() if status == False]
if len(all_stages) !=0:
print(all_stages)
select_ = random.choice(all_stages)
print("\tselected stage: ",select_)
stages[select_] = True
if len(all_stages) == 1:
select_ = random.choice([stage for stage, status in stages.items() if status == True])
stages[select_] = False
process(stages)
else:
print("Done")
print(stages)
process(stages)
uj5u.com熱心網友回復:
代碼是來自@NoBlockhit 的建議
stages = {"stage_1": False, "stage_2":False, "stage_3":False,"state_4": False}
status = True
def process(stages):
global status
print(status)
all_stages = [stage for stage, status in stages.items() if status == False]
if len(all_stages) !=0:
print(all_stages)
select_ = random.choice(all_stages)
print("\tselected stage: ",select_)
stages[select_] = True
if status:
if len(all_stages) == 1:
select_ = random.choice([stage for stage, status in stages.items() if status == True])
stages[select_] = False
status = False
process(stages)
else:
print("Done")
print(stages)
process(stages)
uj5u.com熱心網友回復:
與許多遞回問題一樣,在函式呼叫中添加狀態會對您有很大幫助。
考慮對其進行內部呼叫process_internal()將獲得遞回的當前狀態,無論是階段 0 (False -> True)、階段 1 (True -> False) 還是階段 2 (False -> True)。
import random
def flip_random_state(stages, relevant_bool_val):
[print(f"Stage: {key} with status {value}") for key, value in stages.items()]
relevant_stages = [stage for stage, status in stages.items() if status == relevant_bool_val]
selected_stage = random.choice(relevant_stages)
print(f"\tSelected stage: {selected_stage}\n")
stages[selected_stage] = not stages[selected_stage]
return stages
def count_stages_with_status(stages, input_status):
return len([stage for stage, status in stages.items() if status == input_status])
def process_internal(stages, state):
print(f"State: {state}")
if state == 2 and count_stages_with_status(stages, True) == 4:
print("Done")
print(stages)
return
if state == 0 or state == 2:
relevant_bool_val = False
else:
relevant_bool_val = True
if state == 1 and count_stages_with_status(stages, False) == 4:
state = 2
return process_internal(stages, state)
if state == 0 and count_stages_with_status(stages, True) == 3:
state = 1
return process_internal(stages, state)
stages = flip_random_state(stages, relevant_bool_val)
return process_internal(stages, state)
def process(stages):
return process_internal(stages, 0)
stages = {"stage_1": False, "stage_2": False, "stage_3": False,"state_4": False}
process(stages)
uj5u.com熱心網友回復:
您可以添加一個全域變數作為獲取的標志,無論您是否已經將一個設定回 False,或者添加一個可選引數,如 returnedToFalse=False 并在您最終將其設定為 false 時將其設定為 true。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/466755.html
