本文介绍了NameError:名称“"未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python编写一个脚本来结合西班牙语动词.这是我第一次使用Python编写脚本,因此这可能是一个简单的错误.运行脚本时,我输入"yo tener"并收到错误:

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:

  • 更多信息,请参见: 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])
    

  • 推荐答案

    使用 eval() 函数,您正在将其参数评估为Python语句.我不认为那是你想要做的...

    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...

    如果您试图将代词添加到conjugateForm变量中,并将动词添加到infinitiveVerb变量中,只需使用:

    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()
    

    默认情况下,split()在空白处分割,因此' '不是必需的.

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

    这篇关于NameError:名称“"未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:06
查看更多