2 回答

TA貢獻1827條經驗 獲得超8個贊
用 f-string 打印
f-Strings:一種在 Python 中格式化字符串的新改進方法
PEP 498 - 文字字符串插值
# replace print(x,'runs through',+y+ '.')
# with
print(f'{x} runs through {y}.'
# or with
print(x,'runs through ' +y+ '.')? # note the added space after through and the removal of the ,
更新腳本
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
? ? print(f'{x} runs through {y}.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
? ? print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
? ? print(x)
[out]:
TPLEX runs through Pangasinan.
SLEX runs through Subic.
Cavitex runs through Bacoor,Cavite.
MCX runs through Muntinlupa.
Star Tollway runs through Laguna.
The following Expressway are included in this data set:
TPLEX
SLEX
Cavitex
MCX
Star Tollway
The following Provinces are included in this data set:
Pangasinan
Subic
Bacoor,Cavite
Muntinlupa
Laguna

TA貢獻1946條經驗 獲得超3個贊
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
print(x,'runs through'+y+ '.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
print(x)
您應該刪除 +y+ 旁邊的逗號。
添加回答
舉報