2013-12-13 3 views
1

스페인어 동사를 사용할 스크립트를 Python으로 작성하려고합니다. 이것은 파이썬으로 처음 스크립팅 한 것이므로 간단한 실수 일 수 있습니다. 내가 스크립트, I 입력 "요 tener"를 실행하고 오류가 발생하는 경우 : NameError : name ''이 (가) 정의되지 않았습니다.

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

    은 자세한에서 : 당신이 eval() 기능을 사용 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

, 당신 파이썬 선언문을 파이썬 선언문으로 평가하고있다. 당신이 conjugateForm 변수로 대명사를 얻을하려는 경우 나는

... 그게 당신이 원하는 것을 생각하지 않으며, infinitiveVerb 변수로 동사, 그냥 사용

conjugateForm, infinitiveVerb = text.split() 

기본적으로 split()은 공백으로 나뉘어 있으므로 ' '은 필요하지 않습니다.

1

사용자가 프로그램 내부로 액세스 할 수있게하는 것보다 키를 문자열로 저장하는 것보다 낫습니다. 그렇다면 eval은 전혀 필요하지 않습니다.

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

유사

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

지금 당신은 단지 두 개의 사전에 키와 사용자의 입력을 사용할 수 있습니다.

(스페인어에 대해 별도의 전통이 있는지는 잘 모르겠지만 일반적으로 단수형을 먼저 나열한 다음 첫 번째, 두 번째, 세 번째 모두에서 복수형을 나열하는 것이 일반적입니다. 두 번째 및 세 번째 사람이 복수 인 경우)

+1

또한 'eval'은 보안상의 위험이 있습니다. 사용자가'os.system ('/ home/hack.sh')'을 프로그램의 입력으로 입력하면 어떻게 될지 생각해보십시오. – tripleee

관련 문제