亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除一元 +: 'str' 的錯誤操作數類型錯誤。在我的循環句子中

如何刪除一元 +: 'str' 的錯誤操作數類型錯誤。在我的循環句子中

慕妹3146593 2023-06-20 15:57:46
所以我幾乎把這段代碼寫對了,因為它只是關于字典的。我遇到的唯一問題是錯誤是一元 + 的錯誤操作數類型:'str'。這是我的代碼: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)追溯---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-5-782fcd7b686e> in <module>      1 express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}      2 for x,y in express_file.items():----> 3     print(x,'runs through',+y+ '.')      4 print('The following Expressway are included in this data set:')      5 for x in express_file.keys():TypeError: bad operand type for unary +: 'str'
查看完整描述

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


查看完整回答
反對 回復 2023-06-20
?
智慧大石

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+ 旁邊的逗號。


查看完整回答
反對 回復 2023-06-20
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號