ZCYDHHX 发表于 2018-1-15 17:46:33

零基础20讲

word = ''' string2'''
lenth = len(word)
list1= []
for each in range(lenth):
    i=1
    if word == '\n':
      continue
    for i in range(3):
      if word.isupper and word.isupper and word.islower:
            list1.append(each)
print (list1)
            
这样总是报错,说无法stringindex out of range,怎么回事呢

8306最硬 发表于 2018-1-15 17:49:32

中间的word,其中each+i超过了字符串的索引范围

ZCYDHHX 发表于 2018-1-19 16:26:14

8306最硬 发表于 2018-1-15 17:49
中间的word,其中each+i超过了字符串的索引范围

那怎么修改呢?

8306最硬 发表于 2018-1-21 18:48:17

本帖最后由 8306最硬 于 2018-1-21 18:49 编辑

ZCYDHHX 发表于 2018-1-19 16:26
那怎么修改呢?

可以用切片操作,按你的思路改了下
word = ''' string2'''
word = ''.join(word.split())
lenth = len(word)
list1= []
for each in range(lenth):
    if word.isupper()\
    and word.isupper()\
    and word.islower()\
    and word.islower()\
    and word.islower():
            list1.append(word)
print (''.join(list1))
页: [1]
查看完整版本: 零基础20讲