python 2.7.15中我的家庭作业中的索引问题

时间:2019-04-06 14:39:01

标签: python-2.7

我想使用索引的功能,但是我不知道失败的地方

我试图这样做: str [num +(len(str)/ 2)] 而不是这样: str [num + len(str)/ 2]

def ex():
    sum=0
    str=raw_input("enter string")
    for num in range(0,len(str)/2+1):
        if str[num]==str[num+(len(str)/2)]:
            sum+=1
    if sum==len(str)/2:
        print len(str)
    else:
        print "0"




ex()

这是错误:

Traceback (most recent call last):
  File "C:/Users/avrah/.PyCharmCE2018.3/config/scratches/scratch_5.py", line 15, in <module>
    ex()
  File "C:/Users/avrah/.PyCharmCE2018.3/config/scratches/scratch_5.py", line 5, in ex
    if str[num]==str[num+(len(str)/2)]:
IndexError: string index out of range

0 个答案:

没有答案