Reputation: 191
I have a little script that displays my MOTD upon shell login, which is an ASCII pic with text, in a color gradient using the colr library to create the gradient. I wrote a function to randomize a list of colors and when I run in IDLE it will pull a random color from the list. However when I run the script it only appears to return the first element (red). Here is my function:
from random import randint
from colr import Colr as C
def randcolor():
colors = ['red', 'blue', 'green', 'cyan', 'orange']
n = (len(colors))
o = randint(0, n-1)
color = colors[o]
return color
so when run in IDLE here are the results:
>>> randcolor()
'red'
>>> randcolor()
'red'
>>> randcolor()
'green'
>>> randcolor()
'cyan'
>>> randcolor()
'green'
>>> randcolor()
'red'
>>> randcolor()
'orange'
>>> randcolor()
'green'
But when I implement it in my script it appears to only return the first element:
f = open('/etc/motd', 'r')
motd = f.readlines()
f.close()
print(str(C(' '.join(motd)).gradient(name=randcolor()))
I can't add the results from the script here but suffice it to say that it is not changing the gradient. What is going wrong?
Also I was wondering if this could be done as a lambda. I tried to just assign a variable: color = colors[(randint(0, len(colors)-1))]
but that only returned the first element as the script is doing. Any suggestions? Thanks!
Upvotes: 0
Views: 284
Reputation: 191
Thanks to Steven for the random.choice() solution.
print(str(C(' '.join(motd)).gradient(name=random.choice(colors))))
Upvotes: 1