这里是我的问题:返回一个特定的字符
我在使用EDX Python中的教程来的。练习要求我:
编写函数称为third_index,它接受一个字符串作为参数,并返回字符串的第三个字符。如果用户输入少于3个字符的字符串,则返回“太短”。
def third_index(myString):
if len(myString) >= 3:
for i in myString:
count = 0
while not count == 3:
count =+ 1
return i
else:
return "too short" # Write your function here!
test1 = third_index("Hello World!")
print(test1)
当我尝试运行此,我得到这样的结果: ^ h
我的理论是,循环无法正常工作。它停在第一个循环。
感谢您的帮助,专家! :)
尝试将'my in myString:'更改为'return myString [2]'。字符串有索引,从零开始。所以指数2指的是第三个位置(0,1,2)。你可以把索引称为'something [index]'。 – Mikael
你的代码是错误的...在for循环中,当处理第一个字符时,你只需返回。 while循环什么都不做,只是将计数加到3. – Yang
谢谢你们!帮助我成为你们中的一员。希望能在未来赶上你。 – lloydyu24