布尔在Python中不会发生变化

时间:2014-04-29 09:33:54

标签: python-3.x boolean sentencecase

我试图在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,除了第一个循环。

1 个答案:

答案 0 :(得分:1)

请勿使用is来比较字符串,请使用==

letter is '.' or '!' or '?' or ')'应该是

letter == '.' or letter == '!' or letter == '?' or letter == ')'

OR

letter in '.!?)'