1
votes

I am trying to make a script in Python that will conjugate Spanish verbs. This is my first time scripting with Python, so it may be a simple mistake. When I run the script, I input "yo tener" and receive an error:

Traceback (most recent call last): File "", line 13, in File "", line 1, in NameError: name 'yo' is not defined

  • See more at: http://pythonfiddle.com/#sthash.bqGWCZsu.dpuf

    # Input pronoun and verb for conjugation.
    text = raw_input()
    splitText = text.split(' ')
    conjugateForm = eval(splitText[0]) 
    infinitiveVerb = eval(splitText[1]) 
    
    # Set the pronouns to item values in the list.
    yo = 0
    nosotros = 1
    tu = 2
    el = 3
    ella = 3
    usted = 3
    
    # Conjugations of the verbs.
    tener = ["tengo", "tenemos", "tienes", "tiene", "tienen"]
    ser = ["soy", "somos", "eres", "es", "son"]
    estar = ["estoy", "estamos", "estas", "esta", "estan"]
    
    # List of all of the infinitive verbs being used. Implemented in the following "if" statement.
    infinitiveVerbs = [tener, ser, estar]
    
    # Check to make sure the infinitive is in the dictionary, if so conjugate the verb and print.
    if infinitiveVerb in infinitiveVerbs:
        print("Your conjugated verb is: " + infinitiveVerb[conjugateForm])
    
2

2 Answers

2
votes

When you use the eval() function, you're evaluating its arguments as a Python statement. I don't think that's what you want to do...

If you're trying to get the pronoun into the conjugateForm variable, and the verb into the infinitiveVerb variable, just use:

conjugateForm, infinitiveVerb = text.split()

By default, split() splits on whitespace, so the ' ' isn't necessary.

1
votes

Better than to allow the user access to your program's internals is to store the keys as strings as well. Then you don't need eval at all.

pronouns = { "yo": 0, "nosotros": 1, "tu"; 2, "el": 3, "ella": 3, "usted": 3 }

and similarly

verbs = { "tener": [ "tengo", "tenemos", ... ],
    "ser": [ "soy", "somos", ... ],
    ... }

Now you can just use the user's inputs as keys into the two dictionaries.

(I don't know if there's a separate tradition for Spanish, but a common arrangement is to list the singular forms first, then the plurals, both in first, second, and third person. You seem to be missing the second and third person plural.)