#原程序如下
def test():
print("please input the string")
a = input()
print("please input the word you want to check")
b = input()
c = -1
while True:
pos = a.find(b, c+1)
if pos == -1:
print(c)
break
c = pos
输入str= 'wewewefffff'
检索f, 返回最后一个f的位置10 查询终端pos=8
问题有两个
(1) 最后pos不应该和c保持一致也是8吗? 如果我在最后一行输入print(c,pos) 进行监控,最后一行显示的是10,10
(2) c = pos 这句我上移至查找语句后为什么不行
#为什么while循环里我写成如下会有问题
while True:
pos = a.find(b, c+1)
c = pos # 把这句上移至这里
if pos == -1:
print(c)
break
#
最后终端查询pos=8, 输出c还是-1 , 我在最后一行输入pint(c,pos) 监控,最后一行也是这场的10,10 。这个循环也是跑完了,但为什么会返回的c是-1呢? 当最后一个循环应该是这样的, pos =a.find(b, 10+1) = -1 然后if 判断 pos == -1 ,然后 print(c)应该还是10啊,然后break 。 请问大家这样理解问题在哪?
难道说在 while 外部循环内的c 和 IF 判断里的 c 不是一个? 看起来if 里print(c) 调用的是 c = -1?