我正在尝试在 Python 中创建一个程序,要求用户输入一个字符串(最好是小写),然后将该字符串转换为句子大小写。然而,我用来检查下一个字母是否需要大写的布尔值不会设置为 False,尽管满足“if”语句所需的条件。
class SentenceCaseProgram(object):
def __init__(self, isPunctuation, isSpace, sentence, new_sentence):
self.isPunctuation = isPunctuation
self.sentence = sentence
self.new_sentence = new_sentence
self.isSpace = isSpace
self.count = 0
def Input(self):
self.sentence = str(input("Type in a sentence (with punctuation) entirely in lowercase. "))
def SentenceCase(self):
for letter in self.sentence:
print(self.isPunctuation)
if self.count == 0:
letter = letter.capitalize()
if letter is ' ':
self.isSpace = True
if (self.isPunctuation == True) and (letter in 'abcdefghijklmnopqrstuvwxyz'):
letter = letter.capitalize()
self.isPunctuation = letter is '.' or '!' or '?' or ')'
if letter is 'i' and self.isSpace is True:
letter = letter.capitalize()
self.isSpace = False
self.count += 1
if letter == '.' or '!' or '?' or ')':
self.isPunctuation = True
else:
self.isPunctuation = False
self.new_sentence += letter
def Print(self):
print("Your sentence in sentence case is '%s'" % self.new_sentence)
def Main(self):
self.__init__(False, False, "", "")
self.Input()
self.SentenceCase()
self.Print()
app = SentenceCaseProgram(False, False, "", "")
app.Main()
当我运行程序时,程序要求输入,然后将句子中的每个字母大写,并且 self.isPunctuation 布尔值将不断设置为 True,除了第一个循环。