2013-12-13 114 views
1

我正在尝试在Python中制作一个脚本,它将结合西班牙语动词。这是我第一次使用Python编写脚本,所以这可能是一个简单的错误。当我运行该脚本,我输入 “哟特纳”,并收到一个错误:NameError:名称''未定义

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

  • 多见于: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

当您使用eval()功能,正在评估它的参数是一个Python语句。我不认为这是你想要做什么......

如果你想获得的代名词进入conjugateForm变量,而动词进入infinitiveVerb变量,只需使用:

conjugateForm, infinitiveVerb = text.split() 

默认情况下,split()以空格分隔,因此' '不是必需的。

1

比允许用户访问程序的内部结构更好的是将键存储为字符串。那么你根本不需要eval

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

,同样

​​

现在你可以使用用户的输入,键进入两个库。

(我不知道西班牙语是否有单独的传统,但常见的安排是先列出单数形式,然后是复数形式,无论是第一,第二和第三人。第二和第三人复数)。

+1

此外,'eval'是一个安全风险。考虑一下,如果用户输入'os.system('/ home/hack.sh')'作为程序的输入,会发生什么。 – tripleee