Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 51 · Projects and Practice

Project: build a Spanish verb conjugator

Build a Spanish verb conjugator in Python for the present, preterite, imperfect and future, with irregular verbs and a quiz that catches missing accents.

In this project you will turn what you know about Spanish verbs into a small working program. A conjugator is a perfect first project: the rules are clear, the result is useful and you can test it against the tables you have learned. You will write it in Python, which runs on this page, so you can change the code and see the answer at once. No programming background is required beyond reading simple code, because the program is built from lists, dictionaries and one function.

A path with four steps: endings, the conjugate function, irregular verbs and a quiz.
The conjugator in four steps.

What you will build

By the end you will have a program that:

  • stores the regular endings of four tenses (present, preterite, imperfect, future);
  • conjugates any regular -ar, -er or -ir verb;
  • knows a handful of irregular verbs and falls back on the regular rules for the rest;
  • grades answers, and recognizes when a student only forgot an accent.

Key ideas

Dictionary: a Python structure that maps keys to values (a tense name to its endings). Function: a named block of code that takes inputs and returns a result. Irregular lookup: a table checked before the regular rule. Normalization: removing accents so that two strings can be compared loosely. Test case: a known input with a known correct output.

Step 1: store the rules

Every Spanish regular verb has a stem (the infinitive without -ar, -er, -ir) and a set of endings that depends on the tense and the verb type. That suggests a dictionary like ENDINGS["present"]["ar"], a list of six endings, one for each person. The future is special: its endings attach to the whole infinitive, so a single list is enough.

The program therefore needs one function that picks the right list and glues the endings to the stem:

Python Step 1: regular verbs

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the output and compare it with the tables of chapters 4 to 7. You should see, for example, hablé, hablaste, habló in the preterite and comeremos in the future. If you want to test your understanding, predict the nosotros form of vivir in the imperfect before you run it: it should be vivíamos.

Step 2: irregular verbs and a quiz

A real conjugator needs to know that ser is soy, eres, es and not so, ses, se. The simplest design is a lookup table with the pair (verb, tense) as key. The function checks that table first and falls back on the regular rule when the pair is missing. Similarly, the future of tener and hacer uses an irregular stem (tendr-, har-), so a small dictionary of future stems handles the exceptions.

The quiz part shows a typical programming idea: compare the student's answer with the correct one, and if they differ, compare again after stripping accents. That distinguishes a spelling slip (ibamos) from a wrong form (iremos) and lets the program say "almost". Run the complete program:

Python Step 2: irregular verbs and a quiz

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the fifth answer in the sample student list, ibamos, lacks its accent, so the program reports an "almost" instead of a plain error. Edit the student list to see other messages.

How the code works, line by line

The first dictionary, ENDINGS, has two levels: the first key is the tense and the second is the verb type, so ENDINGS["preterite"]["er"] returns the list ["í", "iste", "ió", "imos", "isteis", "ieron"]. The function conjugate takes the last two letters of the infinitive (verb[-2:]) to find the type and everything before them (verb[:-2]) as the stem. The expression [stem + e for e in ...] is a list comprehension: it goes through the six endings and glues each one to the stem. For the future the program skips the stem and uses the whole infinitive, which is why hablar becomes hablaré and not hablé.

In step 2 the function begins with if (verb, tense) in IRREGULAR. Python checks whether the pair is a key in the dictionary; if yes, it returns the stored list immediately, so the regular rule never runs. Only then does it handle the future with its optional irregular stem: FUTURE_STEMS.get(verb, verb) returns the special stem when the verb has one and the infinitive otherwise. This is a very common pattern in programming: a default rule plus a table of exceptions. Grammar itself works like that, which is why the code mirrors the lessons so closely.

Design choices and why

Why tuples as keys? The pair ("ser", "preterite") identifies one set of six forms. It is compact and easy to extend. Why does the program not change stems for verbs like pensar or volver? Because it would need a second layer of rules; this is an invitation for an extension. Why 'él/ella/usted' as a single label? Because the verb forms are identical for those three subjects, and the six-slot structure mirrors the six endings.

Extension ideas

  1. Stem-changing verbs. Add a set of (verb, change) pairs, such as ("pensar", "e", "ie"), and apply the change to every form except nosotros and vosotros in the present.
  2. Spelling changes. Handle the preterite yo form of -car, -gar, -zar verbs (busqué, llegué, empecé).
  3. More irregulars. Add estar, poder, decir, venir to the lookup table, and the irregular future stems from chapter 7.
  4. Reflexive verbs. Add the pronouns me, te, se, nos, os, se in front of the forms when the verb ends in -se.
  5. Command mode. Print affirmative and negative tú commands.
  6. Random quiz. Use Python's random module to choose a verb and a tense and keep score over ten questions.

Debugging tips

If a form looks wrong, check three things: the verb type (is it really -er, or did you type -ar?), the tense name (a typo such as "preterit" raises a KeyError) and the accent (Python treats habló and hablo as different strings). When you add a new irregular verb, add all six forms in the right order: yo, tú, él, nosotros, vosotros, ellos. A short test list helps: write down five forms you know by heart and run conjugate on them.

Common misconceptions

  • "The program understands Spanish." It only applies the rules you gave it; every irregular verb must be added by you.
  • "One rule covers every verb." Spanish has regular and irregular patterns; a conjugator needs both.
  • "If a form looks right it is right." Always check the accents: tú hablas and él habló are different forms.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why does the program check the IRREGULAR table first?

    The irregular entry must override the regular endings.
  2. 2. Why are the future endings attached to the whole infinitive?

    The Spanish future adds -é, -ás, -á... to the entire infinitive.
  3. 3. What does strip_accents help with?

    Comparing without accents separates a spelling slip from a wrong form.
  4. 4. Which future stem does the program use for "tener"?

    Tener has the irregular future stem tendr-: tendré.

Summary

  • A conjugator turns grammar rules into data (dictionaries) and one function.
  • Irregular verbs sit in a lookup table that overrides the regular rule.
  • The future uses the infinitive; other tenses use the stem.
  • A quiz that compares answers with and without accents gives helpful feedback.
  • Extend the program with stem changes, spelling changes and reflexives.

Next: Project: a personal phrasebook