我正在尝试根据同一字典的值为字典的每个键生成语法。下面是我的代码:
my_dict={'key1':('a','b','c','d'), 'key2':('e','f','c','h'}
global_syntax=""
for key, val in my_dict.iteritems():
if val[2][0]='c':
mysyntax(key)
def write_to_file (syntax_to_write):
global global_syntax
global_syntax=global_syntax+str(syntax_to_write)
def mysyntax(key):
my_variable=key
my_syntax="""
do all stuff + {key} + {val}
""".format(\
key=key,\
val=val[2][0])
write_to_file(mysyntax.encode('utf8'))
所以在这里,在生成的文件中,我想要:
执行所有任务键1 c
做所有东西key2 c
但是当我运行它时,这告诉我未定义mysyntax。因为我从字典中读取数据,所以我们有什么特殊性吗?
答案 0 :(得分:0)
# define your function before you are calling it
def mysyntax(key):
my_variable=key
my_syntax="\"" # to use quotation marks inside quotation marks use "\"" instead of """
my_dict={'key1':('a','b','c','d'), 'key2':('e','f','c','h')}
global_syntax=""
for key, val in my_dict.items():
if val[2][0]=='c': # to Equality Check Difference use == instead of =
mysyntax(key)