lundi 23 mars 2020

Print inside a If statement - Pyhton

(Sorry for any mistakes, english is my second language and I'm still learning)

I'm trying to automate some things in my guitar warmups and scales practice and got stuck at this point. First I wrote this code to randomly select three finger patterns and that the chosen items in the set would be selected again only after all the other items be chosen, but got nothing in fingerPatternLoop.txt and nothing at terminal.

import random

fingerPatterns = set(['1, 2, 3, 4', '1, 2, 4, 3', '1, 3, 4, 2', '1, 3, 2, 4', 
'1, 4, 3, 2', '1, 4, 2, 3', '2, 1, 3, 4', '2, 1, 4, 3', '2, 3, 1, 4', 
'2, 3, 4, 1', '2, 4, 3, 1', '2, 4, 1, 3', '3, 1, 2, 4', '3, 1, 4, 2', 
'3, 2, 4, 1', '3, 2, 1, 4', '3, 4, 2, 1', '3, 4, 1, 2', '4, 1, 2, 3', 
'4, 1, 3, 2', '4, 2, 1, 3', '4, 2, 3, 1', '4, 3, 1, 2', '4, 3, 2, 1', 
    ])

fingerPatternLoop = open("fingerPatternLoop.txt", "a+")
rand_warmup = random.sample(fingerPatterns, 3)

for rand_warmup in fingerPatternLoop:
    if rand_warmup not in fingerPatternLoop:
        print(rand_warmup)
        print(f"{rand_warmup}", file=fingerPatternLoop)

Removing the for loop made the code work.

print(rand_warmup)
print(f"{rand_warmup}", file=fingerPatternLoop)

But I still can't figure out how to make those prints work inside a for loop that verify if any of the items of the random.sample already occurred and clear the fingerPatternLoop.txt in the case of all 24 items already been chosen.

Aucun commentaire:

Enregistrer un commentaire