change number to count number 1, 2 or 3



  • hi we are looking for a way to dynamically change the countNumber of the words.

    We try to create a new big text starting from 3 pieces of text. The main idea is to take 1 word from each text and loop this until all text is rendered into a new text.

    So -1-word1(of txt1)-2-word1(of txt2)-3-word1(of txt3) and so on. Everything works except the number -1- is not dynamic and has to loop from 1 to 3.

    example now:
    -1-A-1-The-1-The-1-Case-1-Boscombe-1-Red…

    must be:
    -1-A-2-The-3-The-1-Case-2-Boscombe-3-Red…

    See code below

    txt1 = 'A Case of Identity My dear fellow said Sherlock Holmes as we sat on either side of the fire in his lodgings at Baker Street life is infinitely stranger than anything which the mind of man could invent We would not dare to conceive the things which are really mere commonplaces of existence If we could fly out of that window hand in hand hover over this great city gently remove the roofs and peep in at the queer things which are going on the strange coincidences the plannings the cross purposes the wonderful chains of events working through generations and leading to the most outré results it would make all fiction with its conventionalities and foreseen conclusions most stale and unprofitable'
    txt2 = 'The Boscombe Valley Mystery We were seated at breakfast one morning my wife and I when the maid brought in a telegram It was from Sherlock Holmes and ran in this way Have you a couple of days to spare Have just been wired for from the west of England in connection with Boscombe Valley tragedy Shall be glad if you will come with me Air and scenery perfect Leave Paddington by the 11 15 What do you say dear said my wife looking across at me Will you go I really do not know what to say I have a fairly long list at present Oh Anstruther would do your work for you You have been looking a little pale lately'
    txt3 = 'The Red Headed League I had called upon my friend Mr Sherlock Holmes one day in the autumn of last year and found him in deep conversation with a very stout florid faced elderly gentleman with fiery red hair With an apology for my intrusion I was about to withdraw when Holmes pulled me abruptly into the room and closed the door behind me You could not possibly have come at a better time my dear Watson he said cordially I was afraid that you were engaged So I am Very much so Then I can wait in the next room Not at all This gentleman Mr Wilson has been my partner and helper in many of my most successful cases and I have no doubt that he will be of the utmost use to me in yours also The stout gentleman half rose from his chair and gave a bob of greeting with a quick little questioning glance from his small fat encircled eyes Try the settee said Holmes relapsing into his armchair and putting his'
    
    
    # set a canvas size
    size(595, 842)
    
    # break each text into words
    words1 = txt1.split()
    words2 = txt2.split()
    words3 = txt3.split()
    
    # get the biggest amount of words
    wordCount = max(len(words1), len(words2), len(words3))
    
    # make an empty list to collect the combined words
    resultWords = []
    
    # count until the biggest amount of words
    for i in range(wordCount):
        # for each list of words
        for words in [words1, words2, words3]:
            # if the current index is smaller than the amount of words
            if i < len(words):
                # add the word at the current index to the list
                resultWords.append('-'+ '1' +'-'+ words[i])
    
    
    # join combined words into a string
    result = ''.join(resultWords)
    
    print(result)
    # enable hyphenation
    hyphenation(True)
    # set font size
    #fontSize(12)
    # draw text in a box
    #textBox(result, (30, 30, 535, 780))
    


  • @pixelman you can add a counter to the inner loop using enumerate:

    for n, words in enumerate([words1, words2, words3], start=1):
        if i < len(words):
            resultWords.append('-'+ str(n) +'-'+ words[i])
    

Log in to reply