0

Python解讀問題

Hi,各位大神好，小弟初學Python，這段程式碼是照著書打的思考後有一段還是不太懂...所以上來邦幫忙技術版詢問

-------------------------以下程式碼-------------------------
start1 = ["fee" ,"fie" ,"foe"]
rhymes = [
("flop","get a mop"),
("fope","turn the rope"),
("fudge","call the judge"),
("fat","pet the cat"),
("fog","walk the dog"),
("fun","say we're done")
]
start2 = "Someone better"

start1_caps = " ".join([word.capitalize() + "!" for word in start1]) #這段不太懂
for first, second in rhymes:
print(f"{start1_caps}{first.capitalize()}!")
print(f"{start2}{second}.")

-------------------------分隔線-------------------------
start1_caps = " ".join([word.capitalize() + "!" for word in start1])
print(start1_caps)

Fee! Fie! Foe!

ccutmis iT邦高手 4 級 ‧ 2020-12-02 09:14:56 檢舉
var my_arr=Array(0,1,2,3,4,5.....99); //以javasrcript為例

my_arr=[i for i in range(0,100)] # my_arr=[0,1,2....99]

start1_caps = " ".join([word.capitalize() + "!" for word in start1])

start1 = ["fee" ,"fie" ,"foe"]
#print(start1)

start1_caps = []
for word in start1:
start1_caps.append(word.capitalize()+"!")
#print(start1_caps)

print(" ".join(start1_caps))
ccutmis iT邦高手 4 級 ‧ 2020-12-02 10:14:45 檢舉

froce iT邦大師 1 級 ‧ 2020-12-02 10:22:54 檢舉

start1_caps = " ".join(
[word.capitalize() + "!" for word in start1]
)
for ccutmis

for froce
woo小技巧呢！＠＠

2 個回答

1

iT邦大神 1 級 ‧ 2020-12-02 09:17:06

``````start1 = ["fee" ,"fie" ,"foe"]
print(start1)

start1_caps = []
for word in start1:
start1_caps.append(word.capitalize()+"!")
print(start1_caps)

print(" ".join(start1_caps))
``````

3
hokou
iT邦新手 2 級 ‧ 2020-12-02 09:22:32

``````for word in start1
``````

``````start1 = ["fee" ,"fie" ,"foe"]

fee
fie
foe
``````

``````word.capitalize() + "!"

Fee!
Fie!
Foe!
``````

``````" ".join()

Fee! Fie! Foe!
``````

==補充回應的問題
2.但是如果換這種方法(想直接新增到start1)就無法新增驚嘆號到word裡面，應該說在提取word的時候就已經發現他沒有新增"!"了

``````start1 = ["fee" ,"fie" ,"foe"]

for word in start1:
word.capitalize() + "!"
print(word)
a = word.capitalize() + "!"
print(a)
``````

``````fee
fie
foe
``````

``````a = word.capitalize() + "!"
print(a)

Fee!
Fie!
Foe!
``````

3.這方法更慘，直接無限驚嘆號給你看...

``````start1 = ["fee" ,"fie" ,"foe"]

for word in start1:
start1.append(word.capitalize() + "!")
# print(word)
# print(" ".join(start1))
print(start1)
``````

``````print(start1)

['fee', 'fie', 'foe', 'Fee!']
['fee', 'fie', 'foe', 'Fee!', 'Fie!']
['fee', 'fie', 'foe', 'Fee!', 'Fie!', 'Foe!']
['fee', 'fie', 'foe', 'Fee!', 'Fie!', 'Foe!', 'Fee!!']
['fee', 'fie', 'foe', 'Fee!', 'Fie!', 'Foe!', 'Fee!!', 'Fie!!']
['fee', 'fie', 'foe', 'Fee!', 'Fie!', 'Foe!', 'Fee!!', 'Fie!!', 'Foe!!']
...
...
``````

ccutmis iT邦高手 4 級 ‧ 2020-12-02 11:27:23 檢舉

1.發現(新增進去start1_cap的方法)，可以出現驚嘆號
start1 = ["fee" ,"fie" ,"foe"]
start1_cap = []
for word in start1:
start1_cap.append(word.capitalize() + "!")
print(start1_cap)

2.但是如果換這種方法(想直接新增到start1)就無法新增驚嘆號到word裡面，應該說在提取word的時候就已經發現他沒有新增"!"了
start1 = ["fee" ,"fie" ,"foe"]

for word in start1:
word.capitalize() + "!"
print(word)
print(" ".join(start1))

3.這方法更慘，直接無限驚嘆號給你看...
start1 = ["fee" ,"fie" ,"foe"]

for word in start1:
start1.append(word.capitalize() + "!")
print(word)
print(" ".join(start1))

start1會回傳到word加驚嘆號，加入後start1 list後面會直接新增驚嘆號的值，word抓不完又一直新增驚嘆號，所以就形成無限迴圈

hokou iT邦新手 2 級 ‧ 2020-12-02 14:33:47 檢舉

a981406015

(已補上

for word in start1:
a = word.capitalize() + "!" #這邊給它一個變數就可以了
print(a)

hokou iT邦新手 2 級 ‧ 2020-12-02 20:30:10 檢舉