為什么錯了
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
w = 'World'
c= 'China'
b = 'Beijing'
i= 'imooc'
result = template.format(w,c,b,i)
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
w = 'World'
c= 'China'
b = 'Beijing'
i= 'imooc'
result = template.format(w,c,b,i)
2021-12-07
舉報
2021-12-08
因為第一行 template = 'Hello {w}, Hello {c}, Hello , Hello {i}.' 里面的w,c,b,i是給該模板位置的代號名稱,而不能作為一個變量,為了不重復,建議將下面賦值語句的變量名進行修改,如
s1 = 'World'
s2 = 'China'
s3 = 'Beijing'
s4 = 'imooc'
然后最后賦值給模塊中的位置
result = template.format(w=s1,c=s2,b=s3,i=s4)
最后要記得打印出來
print(result)
??
2021-12-23
我是新手,我發(fā)現(xiàn),你最后一行變成 result = template.format(w=w,c=c,b=b,i=i),也可以運行