Output text



  • Hello everyone:

    I've been trying to find a way to use the output text as drawn text, but I'm not really winning the battle, so I finished copying the text, here is my code:

    #Set today's time
    day = 29
    month = 8
    year = 2019
    
    date = '%02d/%02d/%04d' % (day, month, year)
    print("Today is " + str(date))
    
    #Birthday function
    def birthDay(name, bDay, bMonth, bYear):
        
        bDate = '%02d/%02d/%04d' % (bDay, bMonth, bYear)
        
        if month >= bMonth and day >= bDay:
            age = year - bYear
            print("I am " + name + " and I have " + str(age) + " years." + " My birthday is " + str(bDate))
        else:
            age2 = year - bYear -1
            print("I am " + name + " and I have " + str(age2) + " years." + " My birthday is " + str(bDate))
    
    #Examples        
    size(1000, 1000)
    
    birthDay("L", 4, 9, 1992)
    birthDay("P", 21, 1, 1993)
    birthDay("E", 15, 9, 1971)
    birthDay("J", 22, 3, 1961)
    
    txt = """Today is 29/08/2019
    
    I am L and I have 26 years. My birthday is 04/09/1992
    
    I am P and I have 26 years. My birthday is 21/01/1993
    
    I am E and I have 47 years. My birthday is 15/09/1971
    
    I am J and I have 58 years. My birthday is 22/03/1961"""
    
    #Image
    
    x, y, w, h = 100, 100, 800, 800
    
    fill(0.5, 1, 1)
    rect(x, y, w, h)
    font('.SFNSRounded-Black', 130)
    fontSize(50)
    fontVariations(GRAD=500, wght= 1000)
    stroke(1, 0, 0)
    strokeWidth(3)
    overflow = textBox(txt, (x, y, w, h), align="center")
    print(overflow)
    
    saveImage('~/Desktop/ages.png')
    
    

    Does anyone know how to make it?
    Thanks.



  • hello @eduairet,

    you can use Python’s string formatting syntax to create strings with variable parts:

    name = 'John'
    age = 17
    
    # old syntax, still works
    txt1 = "I am %s and I have %s years." % (name, age)
    
    # new in py3: f-strings
    txt2 = f"I am {name} and I have {age} years."
    

    and here’s how you can repeat it for a list of names and collect the output into a single text:

    persons = [
        ('Michael', 34),
        ('Maria', 15),
        ('Daniel', 56),
    ]
    
    txt = ''
    for name, age in persons:
        txt += f"I am {name} and I have {age} years.\n"
    
    fontSize(56)
    textBox(txt, (0, 0, width(), height()))
    

    hope this helps!



  • Thanks again @gferreira it worked better that way ☺ now I'm trying to make a list of random objects and names, but I don't want them to be repeated, do you know if there's a similar way to match the pairs without repetition ?

    Here’s the code:

    #Canvas Size
    w = 1000
    h = 1000
    
    def whiteCanvas():
        newPage(w, h)
        fill(1)
        rect(0, 0, w, h)
    
    def blackCanvas():
        newPage(w, h)
        fill(0)
        rect(0, 0, w, h)
    
    def objects(tCol):
        objLuck = randint(0, 3)
        objTxt = "My object is: "
        
        if objLuck == 1:
            txt = objTxt + "Fish"
            
        elif objLuck == 2:
            txt = objTxt + "Bullet"
    
        else:
            txt = objTxt + "Glass"
        
        fontSize(56)  
        fill(tCol)     
        text(txt, (width()/2, 500), align ='center')
    
    def students(tCol):
        stdLuck = randint(0, 3)
        stdTxt = "I am "
        
        if stdLuck == 1:
            txt = stdTxt + "Pedro"
            
        elif stdLuck == 2:
            txt = stdTxt + "Caro"
    
        else:
            txt = stdTxt + "Raúl"
        
        fontSize(56) 
        fill(tCol)      
        text(txt, (width()/2, 400), align ='center')
    
    def pairs():
        Luck = randint(0, 1)
        if Luck == 0:
            whiteCanvas()
            students(0)
            objects(0)
        else:
            blackCanvas()
            students(1)
            objects(1)
            
    for i in range(3):
        pairs()
    

    Thank you very much



  • hello @eduairet,

    you could shuffle each list separately, then combine them using zip():

    from random import shuffle
    
    L1 = ['Michael', 'John', 'Graham']
    L2 = ['spam', 'bacon', 'eggs']
    
    shuffle(L1)
    shuffle(L2)
    
    L3 = list(zip(L1, L2))
    print(L3)
    

    there are other ways to do it… 😉



  • This is totally better @gferreira you’re really kind, thank you. I’ve tried the following code and it’s exactly what I needed, I was just wondering if there is a way to make it with less code, I’ve noticed that you have better results in less lines and that’s really nice. Thanks again.

    #Canvas Size
    
    w = 1000
    h = 1000
    
    yPos = 500
    
    def whiteCanvas():
        newPage(w, h)
        fill(1)
        rect(0, 0, w, h)
    
    def blackCanvas():
        newPage(w, h)
        fill(0)
        rect(0, 0, w, h)    
    
    from random import shuffle
    
    L1 = ['Pedro', 'Jonás', 'Raúl', 'Celestina', 'Constanza']
    L2 = ['Ojos', 'Latas', 'Lombrices', 'Cigarro', 'Guitarra']
    
    shuffle(L1)
    shuffle(L2)
    
    L3 = list(zip(L1, L2))
    print(L3)
    
    def bgCol():
        Luck = randint(0, 1)
        if Luck == 0:
            whiteCanvas()
            fontSize(56)
            fill(0)
        else:
            blackCanvas()
            fontSize(56)
            fill(1)
    
    def pairs():
       bgCol()
       txt = "Pair: " + str(L3[0])
       text(txt, (width()/2, yPos), align ='center')
       
       bgCol()
       txt = "Pair: " + str(L3[1])
       text(txt, (width()/2, yPos), align ='center')
       
       bgCol()
       txt = "Pair: " + str(L3[2])
       text(txt, (width()/2, yPos), align ='center')
       
       bgCol()
       txt = "Pair: " + str(L3[3])
       text(txt, (width()/2, yPos), align ='center')
       
       bgCol()
       txt = "Pair: " + str(L3[4])
       text(txt, (width()/2, yPos), align ='center')
            
    for i in range(1):
        pairs()
        
    saveImage('~/Desktop/ruleta.png', multipage=True)
    


  • @eduairet you’re welcome, happy to help.

    here is a more concise and more pythonic version of your code:

    from random import shuffle
    
    w, h = 1000, 1000
    
    names = ['Pedro', 'Jonás', 'Raúl', 'Celestina', 'Constanza']
    things = ['Ojos', 'Latas', 'Lombrices', 'Cigarro', 'Guitarra']
    
    shuffle(names)
    shuffle(things)
    
    pairs = list(zip(names, things))
    
    def makePage(pair):
    
        # flip a coin to choose colors
        coin = randint(0, 1)
        if coin:
            color1 = 0,    
            color2 = 1,
        else:
            color1 = 1,
            color2 = 0,        
    
        # draw page background
        newPage(w, h)
        fill(*color1)
        rect(0, 0, w, h)
    
        # draw text
        name, word = pair
        fontSize(56)
        fill(*color2)
        text(f"Pair: {name} & {word}", (w/2, h/2), align='center')
    
    # make all pages
    for pair in pairs:
        makePage(pair)
    
    # save pdf to disk
    saveImage('~/Desktop/ruleta.png', multipage=True)
    

    if you don’t understand something, please just ask! cheers



  • @gferreira
    probably not pythonic
    but i was surprised this works
    (if you keep things black and white)

    coin = randint(0, 1)
    color1 = coin,
    color2 = not coin,
    


  • @jo I thought about that too 🙂 but then suddenly Yoda appeared:

    yoda-readability_small.jpg



  • @gferreira thanks, it worked, I'm coming with some new exercises. Cheers.


Log in to reply