2

I have made a Spanish verb conjugation function which has 2 parameters for the pronoun and verb as shown below:

def doConjugate(pronoun,verb):
    if unidecode(verb).endswith("ar"):
        arDict = {"yo": "o", "tú": "as", "usted": "a", "él": "a", "ella": "a", "nosotros": "amos", "vosotros": "áis", "ustedes": "an", "ellos": "an", "ellas": "an"}
        verb = verb[:-2] + arDict[pronoun]
        print(verb)
    if unidecode(verb).endswith("er") or unidecode(verb).endswith("ir"):
        erDict = {"yo": "o", "tú": "es", "usted": "e", "él": "e", "ella": "e", "nosotros": "emos", "vosotros": "éis", "ustedes": "en", "ellos": "en", "ellas": "en"}
        irDict = {"nosotros": "imos", "vosotros": "ís"}
        if (pronoun == "nosotros" or pronoun == "vosotros") and verb.endswith("ir"):
            verb = verb[:-2] + irDict[pronoun]
        else:
            verb = verb[:-2] + erDict[pronoun]
        print(verb)

Addionally, I have two text files. One with all of the verbs, and a second one with all of the pronouns. I want to make a for loop which takes the pronoun from one file and the verb from the another and conjugate it. However, I do not know how to do so.

Below is what it tried to do:

with open("words.txt") as file:
    with open("pronouns.txt") as f:
        for word in file:
            for conjugates in f:
                conjugated = doConjugate(conjugates,word)
                print(conjugated)

However, in the terminal, instead of the conjugations, I just get the word "None" over and over. I am new to python and am requesting someone to show how to do this properly.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Tegh Singh
  • 51
  • 4
  • What is the format of your text files? – Captain Caveman May 28 '22 at 23:34
  • 2
    The PROBLEM here is that, after you have gone through the inner loop once, your `f` file is at end-of-file. For all the rest of the lines in `file`, there's nothing left to read. Perhaps you should read both files into lists, so you can iterate the lists several times. – Tim Roberts May 28 '22 at 23:35
  • Your function `doConjugate()` prints its results, but does not return any value. But this line: `conjugated = doConjugate(conjugates,word)` clearly expects it to return a value. Put `return verb` instead of `print(verb)` in the function. If you invoke a function from the `>>>` prompt, it can be hard to see what difference this might make. But that is only true because the `>>>` prompt will automatically display a returned value from a function, as `print()` would do, and will not automatically display a returned value if that value is `None`. – BoarGules May 29 '22 at 00:19

2 Answers2

1

You could save the contents of each text files into a list, then manipulate using these lists in memory

with open("words.txt") as file:
    words_list = file.read().splitlines()
    
with open("pronouns.txt") as f:
    pronouns_list = f.read().splitlines()

for word in words_list:
    for conjugates in pronouns_list:
        conjugated = doConjugate(conjugates,word)
        print(conjugated)
blackraven
  • 5,284
  • 7
  • 19
  • 45
1

Something like this should do it.

words = [k.strip() for k in open("words.txt").readlines()]
prons = [k.strip() for k in open("pronouns.txt").readlines()]
for w,p in itertools.product(words,prons):
    conjugated = doConjugate(p,w)
    print(conjugated)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30