Python 3替换字母

时间:2018-02-21 05:53:17

标签: python-3.x

我需要帮助替换python 3中的字母

错误消息:

  

第24行,在       repl = capitalWord [1:] TypeError:' NoneType'对象不可订阅

我真的想弄明白这个问题,但我真的很沮丧。

Code:
#----------------------------------------------
#-----------------------------------------------
#function to print the numbers and prime numbers
#-----------------------------------------------
import re
inStr = input('Enter input string: ')

# split word separated by sapce in enetered sentence
words = inStr.split(' ')

# traverse list of words anf find capitalized word
capitalWord = None
for word in words:
    if word.isupper()==True:
        capitalWord = word


# replace * with remaining letters of
#capital word after skipping first character

repl = capitalWord[1:]
inStr = re.sub('[*]',repl,inStr)

# join all words with space again and print them
print ('Output sentence :')
print (inStr)

1 个答案:

答案 0 :(得分:0)

import re
inStr = input('Enter input string: ')

# split word separated by sapce in enetered sentence
words = inStr.split(' ')

# traverse list of words anf find capitalized word
capitalWord = None
for word in words:
    if word.isupper()==True:
        capitalWord = word


# replace * with remaining letters of
#capital word after skipping first character
if capitalWord is not None:
  repl = capitalWord[1:]
  inStr = re.sub('[*]',repl,inStr)
  # join all words with space again and print them
  print ('Output sentence :')
  print (inStr)