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

為了賬號安全,請及時綁定郵箱和手機立即綁定

請老師解答

# Enter a code

template='Life is short,you need {P}'

ch='python'

result=template.format(P=ch)

print(result)


#這里,為什么不能直接給P賦值呢?

# Enter a code

template='Life is short,you need {P}'

P='python'

result=template.format(P)

print(result)

#這個程序為什么會出錯呢?

正在回答

2 回答

template='Life?is?short,you?need?{P}'

P='python'

result=template.format(P=P)

print(result)

P相當于占位符,沒有給他賦值

1 回復 有任何疑惑可以回復我~

template='Life is short,you need {P}'這里的P和

P='python'這里的P并不是同一個P

第一個P是給模板里的參數指定一個名字,方便調用

第二個P是變量名

result=template.format(P)這里的P是變量名

改成如下就正確(不指定參數名字):

template='Life is short,you need {}'

P='python'

result=template.format(P)

print(result)

或者改成如下(指定參數名字)

template='Life is short,you need {P}'

P='python'

result=template.format(P=P)

print(result)


這里面的result=template.format(P=P)第一個P是指參數名字,第二個P是變量名

為了避免混淆,一般要區分開來,如下:

template='Life is short,you need {x}'

P='python'

result=template.format(x=P)

print(result)


1 回復 有任何疑惑可以回復我~
#1

嘿呀2021

這個寫的很清楚
2022-11-08 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

請老師解答

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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