Кажется, что даже если я объявлю переменные внутри функции global, они все равно не распознаются в программе main ().
Я пытался объявить переменные, которые моя функция scan (string) возвращает как глобальные, чтобы иметь возможность доступа к ним через main (), но интерпретатор по-прежнему жалуется на них с ошибкой .... global_name is "Имя переменной "не определено.
print "\n--------- // Program Lexicon // -------------\n"
direction_words = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back', 'forward')
verbs = ('go', 'stop', 'kill', 'eat', 'run')
nouns = ('door', 'bear', 'princess', 'cabinet')
numbers = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
results_list = []
def scan(string):
global result_direction, result_verb, result_noun, result_number, result_error
if string in direction_words:
print "\nfound match in >> direction words <<: %s" %string
result_direction = ('direction', string)
return result_direction
elif string in verbs:
print "\nfound match in >> verbs <<: %s" %string
result_verb = ("verb", string)
return result_verb
elif string in nouns:
print "\nfound match in >> nouns <<: %s" %string
result_noun = ('noun', string)
return result_noun
elif string in numbers:
print "\nfound match in >> numbers <<: %s" %string
result_number = ('number', int(string))
return result_number
elif string is not verbs or nouns or numbers or directions:
print "\nmassive error > string < not matching any condition"
result_error = ('error', string)
return result_error
else:
print "Failure!"
def main():
user_input = raw_input("\nenter your answer please: ")
user_words = user_input.split()
for i in range(len(user_words)):
look_up_words = user_words[i]
scan(user_words[i])
results_list.append(result_direction, result_verb, result_noun, result_number, result_error)
main()
--------- // Program Lexicon // -------------
enter your answer please: south
found match in >> direction words <<: south
Traceback (most recent call last):
File "lexicon.py", line 60, in <module>
main()
File "lexicon.py", line 57, in main
results_list.append(result_direction, result_verb, result_noun, result_number, result_error)
NameError: global name 'result_verb' is not defined
PS D:\Exersize_46\Python27_Projects\Exersize_48\Exersize_48> python lexicon.py
--------- // Program Lexicon // -------------
enter your answer please: south max east
found match in >> direction words <<: south
Traceback (most recent call last):
File "lexicon.py", line 60, in <module>
main()
File "lexicon.py", line 57, in main
results_list.append(result_direction, result_noun, result_number, result_error)
NameError: global name 'result_noun' is not defined
PS D:\Exersize_46\Python27_Projects\Exersize_48\Exersize_48>