dimanche 29 mai 2022

How to print a random word from a list which ends with 's'?

So I'm trying to figure out how to run a script that searches for a random word ending in 's'. the txt list I'm using is 50% words ending in 's'. I have tried a few things and I can't seem to figure it out. basically what I want it to do is run 'snoun' and tell it hits a word ending in s. I'm sure there are other ways to achieve this one thing, but the process itself is relatively important. right now I'm getting RecursionError: maximum recursion depth exceeded in comparison.

X = random.randint(1,218)
file = open('pluralnoun.txt')
content = file.readlines()
snoun = content[X]
def nouns():
    if snoun == "*s":
        print(snoun)
    else:
        nouns()

After seeing a comment I realize of course it won't work. So I tried

def run():
    X = random.randint(1,218)
    file = open('pluralnoun.txt')
    content = file.readlines()
    snoun = content[X]
    if snoun == "*s":
        print(snoun)
    else:
        run()

and I am getting IndexError: list index out of range




Aucun commentaire:

Enregistrer un commentaire