# Write a function called "position" that returns a tuple of the first
uppercase letter and its index location. If not found, returns -1.
剛剛在寫這個很簡單的
def position(string):
for num, s in enumerate(string): # enumerate (iterable, start=0)
if s == s.upper():
print((s, num))
return (s, num)
print(-1)
return -1
enumerate很好寫沒有問題
但我剛剛想說用index寫寫看
def position(string):
for index in range(0, len(string)):
if string[index] == string[index].upper():
print((string[index], index))
print(-1)
return -1
position("abcd") # returns -1
position("AbcD") # returns ('A', 0)
position("abCD") # returns ('C', 2)
理論上上下兩者return都會這樣
但後者卻長這樣
('A', 0)
('D', 3)
-1
('C', 2)
('D', 3)
-1
def position(string):
for index in range(0, len(string)):
if string[index] == string[index].upper():
print((string[index], index))
return (string[index], index)
print(-1)
return -1
我補上return這行才正確
-1
('A', 0)
('C', 2)
有沒有人可以跟土法煉鋼學習的我解釋一下這個return扮演甚麼角色
我只是很直覺得加上去,就寫對了==
不懂why