gpt4 book ai didi

python - NameError:名称 '' 未定义

转载 作者:行者123 更新时间:2023-11-30 23:30:28 25 4
gpt4 key购买 nike

我正在尝试用 Python 编写一个脚本来结合西类牙语动词。这是我第一次使用 Python 编写脚本,所以这可能是一个简单的错误。当我运行脚本时,我输入“yo tener”并收到错误:

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

最佳答案

当您使用eval()时函数,您将其参数作为 Python 语句进行评估。我认为这不是您想要做的...

如果您尝试将代词放入 conjugateForm 变量中,并将动词放入 infinitiveVerb 变量中,只需使用:

conjugateForm, infinitiveVerb = text.split()

默认情况下,split() 按空格进行拆分,因此不需要 ' '

关于python - NameError:名称 '' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20558934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com