我想做一些与此功能等同的事情:
my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
menu_option = raw_input(
"Which option would you like to configure [0]?n"
"[0] NO CHANGESn"
"[1] Option1: t{0}n".format(my_dict.get('option1'))
"[2] Option2: t{0}n".format(my_dict.get('option2'))
) or "0"
或
my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
menu_option = raw_input(
"Which option would you like to configure [0]?n"
"[0] NO CHANGESn"
"[1] Option1: t %s n" % my_dict.get('option1')
"[2] Option2: t %s n" % my_dict.get('option2')
) or "0"
运行my_func((的结果如下:
Which option would you like to configure [0]?
[0] NO CHANGES
[1] Option1: VALUE1
[2] Option2: VALUE2
我遇到了无效的语法错误。有办法做到吗?
在将其与format
调用组合时,您使用的是多行字符串;使用具有单一格式的多行字符串
menu_option = raw_input("""
Which option would you like to configure [0]?
[0] NO CHANGES
[1] Option1: t{0}
[2] Option2: t{1}
""".format(my_dict.get('option1'), my_dict.get('option2'))
) or "0"
或添加串联操作员
menu_option = raw_input(
"Which option would you like to configure [0]?n" +
"[0] NO CHANGESn" +
"[1] Option1: t{0}n".format(my_dict.get('option1') +
"[2] Option2: t{0}n".format(my_dict.get('option2')
) or "0"
用 """
编写多行注:
my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
menu_option = raw_input(
"""Which option would you like to configure [0]?
[0] NO CHANGES
[1] Option1: t{0}
[2] Option2: t{1}n""".format(my_dict.get('option1'), my_dict.get('option2'))
) or "0"