# 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