Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is python dictionary not refilling in while loop?
    primarykey
    data
    text
    <p>I've got a database that I loop through, and based on certain conditions, I send the relevant database entries to a dictionary that I created. I then want to pick one randomly from this dictionary, store it in a list, clear the dictionary, and do the whole thing again with incremented variables. FYI, the ['StartNoteNum'], etc. are just column names within the cost database.</p> <p>What happens though, is that it works fine the first time thorough the loop, but if I try to clear the dictionary anywhere in the code (inside or outside the while loop), then the dictionary never re-fills based on the incremented values even though it should. To confirm that it should re-fill properly, I have set the initial values to be all possible values that it would encounter in the while loop, and each one works the first time through the loop, but fails once it tries to cycle. The error I get is that the random function can't pull from an empty dictionary. Grr... here is the code.</p> <pre><code>def compute_policy(clean_midi, cost_database): note = 0 #Setting up starting variables. total_score = [] current_finger = 1 path = [1] next_move = {} while note &lt;= 2: current_note = clean_midi[note] #get note-pair for scoring dest_note = clean_midi[note+1] for each in cost_database: #find all relevant DB entries if (int(each['StartNoteNum']) == current_note and int(each['DestNoteNum']) == dest_note and int(each['StartFing']) == current_finger): next_move[int(each['DestFing'])] = int(each['Score']) #move relevant entries to separate "bin" policy_choice = random.choice(next_move.keys()) #choose one at random total_score.append(next_move[policy_choice]) #track the scores for each choice in a list path.append(policy_choice) #track the chosen finger current_finger = policy_choice #update finger variable note += 1 path.append(current_finger) #append last finger since the loop won't run again return total_score, path </code></pre> <p>any help here would be much appreciated. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload