在这个python脚本中,当有多个句子时,我的逻辑错误在哪里?

在这个python脚本中,当有多个句子时,我的逻辑错误在哪里?,python,Python,我有一个逻辑错误找不到。我试图把每个句子的第一个字母大写。我很难理解如何继续下一个句子以大写第一个字母。我该如何进入下一句?如果你看到我的错误,你能告诉我是什么吗 def main(): # get a string from the user stringAsked = input('Please enter a short statement consisting of a couple or more sentences ') # call the capitalize func

我有一个逻辑错误找不到。我试图把每个句子的第一个字母大写。我很难理解如何继续下一个句子以大写第一个字母。我该如何进入下一句?如果你看到我的错误,你能告诉我是什么吗

def main():

# get a string from the user
   stringAsked = input('Please enter a short statement consisting of a couple or more sentences ')

# call the capitalize function
   modifiedString = capitalize(stringAsked)

# Print the string
   print(modifiedString)


def capitalize(my_string):
    # Initialize string and counter variables
    modString = ''
    ind = 0

    # Create while loop to cycle through each character of the string
    while ind < len(my_string):

        # Assign Current character to the ch variable
        ch = my_string[ind]

        # If first character of string
        if ind == 0:

            # Store capitalized character in return variable
            modString += ch.upper()

        # Check if the character is at the end of the string
        elif ch == '.' or ch == '!' or ch == '?':

            # add current character to the return variable
            modString += ch

            # If more than one sentence
        if ind + 2 < len(my_string):

        # Add the space
            modString += my_string[ind + 1]

            # Capitalize the first character
            modString += my_string[ind + 2].upper()

            # Increment the index
            ind = ind + 2

        # If the character is in the middle of a sentence
        else:

            #Add the character to return variable
            modString += ch

        #increment the index to go to next character
        ind = ind + 1

    # return the variable to calling function
    return modString

main()
def main():
#从用户处获取字符串
STRINGASQUESD=input('请输入由两个或多个句子组成的简短语句')
#调用大写函数
modifiedString=大写(StringAsk)
#打印字符串
打印(修改字符串)
def大写(我的字符串):
#初始化字符串和计数器变量
modString=''
ind=0
#创建while循环以循环遍历字符串的每个字符
而ind
您对代码的缩进(关于在“.”之后做什么)似乎是个问题。您需要对句号(或类似句号)后的空格和字符进行缩进处理,以便它在“.”条件内执行,而不是作为同一级别的另一个条件执行

def main():

# get a string from the user
   stringAsked = input('Please enter a short statement consisting of a couple or more sentences ')

# call the capitalize function
   modifiedString = capitalize(stringAsked)

# Print the string
   print(modifiedString)


def capitalize(my_string):
    # Initialize string and counter variables
    modString = ''
    ind = 0

    # Create while loop to cycle through each character of the string
    while ind < len(my_string):

        # Assign Current character to the ch variable
        ch = my_string[ind]

        # If first character of string
        if ind == 0:

            # Store capitalized character in return variable
            modString += ch.upper()

        # Check if the character is at the end of the string
        elif ch == '.' or ch == '!' or ch == '?':

            # add current character to the return variable
            modString += ch

            # If more than one sentence
            if ind + 2 < len(my_string):

                # Add the space
                modString += my_string[ind + 1]

                # Capitalize the first character
                modString += my_string[ind + 2].upper()

                # Increment the index
                ind = ind + 2

        # If the character is in the middle of a sentence
        else:

            #Add the character to return variable
            modString += ch

        #increment the index to go to next character
        ind = ind + 1

    # return the variable to calling function
    return modString

main()
def main():
#从用户处获取字符串
STRINGASQUESD=input('请输入由两个或多个句子组成的简短语句')
#调用大写函数
modifiedString=大写(StringAsk)
#打印字符串
打印(修改字符串)
def大写(我的字符串):
#初始化字符串和计数器变量
modString=''
ind=0
#创建while循环以循环遍历字符串的每个字符
而ind

希望这有意义

这个怎么样-首先在每个“.”处拆分字符串,然后遍历每个句子并将第一个字母转换为大写字母

s = "hello world. please help me. can we do this simpler?"
stringlist = s.split(". ")

def first_char_upper(stringlist):
  for i in range(0, len(stringlist)):
    stringlist[i] = stringlist[i][0].upper() + stringlist[i][1:]
  return stringlist

print(first_char_upper(stringlist))

输出:
['Hello world',“Please help me',“我们能做得更简单些吗?”]

您可以使用正则表达式将字符串拆分为句子,而不是逐个字符。检查
如果ind+2
只需要在检测到“.”之后才可以执行。在代码中,检查
index==0
,然后检查
elif ch=='.'或ch='!'或者ch='?':
然后检查
如果ind+2
。如果
elif ch=='.”或ch=='!'或者ch='?':
为true,因此代码需要位于该if statemet中