我正在尝试打印字典值
sales_record = {
'price': 3.24,
'num_items': 4,
'person': 'xyz'}
sales_statement = 'sales_record['person'] got sales_record['num_items'] item(s) at a cost of sales_record['price']each for a total of sales_record['price']*sales_record['num_items']'
print(sales_statement)
但这只是给我一个错误
File "<ipython-input-48-9c532ca6dcd1>", line 6
sales_statement = 'sales_record['person'] got sales_record['num_items'] item(s) at a cost of sales_record['price']each for a total of sales_record['price']*sales_record['num_items']'
答案 0 :(得分:2)
如果您使用的是最新版本的Python(3.6或更高版本),请将该行替换为:
sales_statement = f"{sales_record['person']} got {sales_record['num_items']} item(s) at a cost of {sales_record['price']} each for a total of {sales_record['price']*sales_record['num_items']}"
答案 1 :(得分:1)
您需要格式化字符串。这将适用于新旧版本的Python:
G.add_edge(149, 88)
G.add_edge(139, 168)
G.add_edge(215, 218)
G.add_edge(218, 215)
G.add_edge(429, 400)
G.add_edge(400, 429)
G.add_edge(207, 176)
G.add_edge(176, 207)
G.add_edge(31, 45)
G.add_edge(45, 31)
G.add_edge(411, 381)
G.add_edge(381, 411)
G.add_edge(393, 335)
G.add_edge(335, 393)
G.add_edge(287, 317)
G.add_edge(317, 287)
G.add_edge(41, 77)
...
答案 2 :(得分:0)
基本上是因为引号。
Container(
width : double.infinity,
height : 150,
child : SliderTheme.of(context).copyWith(
trackHeight :21 ,
activeTrackColor: Colors.green,
activeTickMarkColor: Colors.red,
overlayColor: Colors.yellow,
thumbColor: Colors.brown,
inactiveTrackColor: Colors.black,
showValueIndicator: ShowValueIndicator.always,
child : frs.RangeSlider(
min: 0,
max: 1000,
lowerValue: 0,
upperValue: 100,
showValueIndicator: true,
divisions: 20,
onChanged: (val0,val1){
settheState(val0);
},
)
) )
答案 3 :(得分:0)
您可以对全字符串使用双引号,对字典键使用单引号,以使它们之间没有混淆。在Python 3.6或更高版本中。您可以使用f字符串,并在字符串前加上字母f来将变量括在花括号中。在此之前,您将使用格式字符串方法。
sales_record = {
'price': 3.24,
'num_items': 4,
'person': 'xyz'
}
sales_statement = f"{sales_record['person']} got {sales_record['num_items']} item(s) " \
+ f"at a cost of {sales_record['price']} each for a total of " \
+ f"{sales_record['price'] * sales_record['num_items']}"
print(sales_statement)