编写一个提示用户输入句子的程序

时间:2018-11-16 15:48:12

标签: python python-3.x

  

编写一个程序,提示用户输入句子。然后检查此句子以确保该句子的第一个单词大写,并且该句子以标点符号结尾。如果书写不正确,请修复该句子,打印错误类型,然后打印固定的句子。

我正在遵循为此类提供的说明,并在第四行代码中始终收到无效的语法错误。想知道是否有人知道为什么并且可以向我展示如何修复它或以其他方式编写该程序。

import string

sentence = input("Enter a sentence ")

class acceptSentence():

    punctuationcount = lambda a,b:len(list(filter(lambda c: c in b,a)))

    numberofpunctuationcount =  punctuationcount(sentence,string.punctuation)

for each in sentence:
    if each.startswith(each.upper()):
        print ("Starts with Capital letter ",each)

        break

    if (numberofpunctuations >=1):

        print("Sentence Ends with punctuation")

    else:
        print("Error : there is no punctuion mark at end of setence")


        obj = acceptSentence()
        obj.calculate(sentence)

3 个答案:

答案 0 :(得分:0)

只需:

sentence = input("Enter a sentence ").lstrip()  # remove trailing whitespaces

# check if first character is uppercase
if not sentence[0].isupper():
    print("Sentence does not start with uppercase character")
    # correct the sentence
    sentence = sentence[0].upper() + sentence[1:]

# check if last character is a punctuation 
# (feel free to add other punctuations)
if sentence[-1] not in (['.']):
    print("Sentence does not end with punctuation character")
    # correct the sentence
    sentence += '.'

#finally print the correct sentence
print(sentence)

答案 1 :(得分:0)

基于描述,您可能会过分考虑这项任务:该任务只涉及一个句子,然后只需确保第一个字母是大写字母,并且在末尾使用标点符号即可。

def sentence():
  text=input("Please type a sentence here: ")

  if text[0].isalpha() and not text[0].isupper(): # Begins with letter, but not uppercase?
    text=text[0].upper()+text[1:]                 # Make it uppercase then
    print("Sentences should start in uppercase");

  if text[-1] not in [".","!","?"]:               # Does not end with punctuation?
    text+="."                                     # Append a period then
    print("Sentences should end with punctuation mark")

  return text

它既可以扩展(如将.strip()引入空白,只需将其添加到input行),甚至可以缩短(可以删除第一个if,因为在那里在已经大写的东西上调用.upper()并没有错。但是,由于必须打印错误,因此if必须保留在这种特殊情况下。

答案 2 :(得分:0)

您的代码未正确缩进,这是执行代码时导致IndentationError的原因。由于“ lambda”的拼写错误,您的lambda过滤器还会吐出SyntaxError。您的if-else语句也未对齐,请小心缩进代码。

这是您程序的更简单替代方案:

import string

sentence = input("Enter a sentence:")

first_word = sentence.split()[0] # .split() gives you a list of words in the sentence, 0 is the index of the first word;

capitalized_first_word = first_word.title() # .title() capitalizes a string;

# Check whether the first word is not equal to the capitalized word:
if first_word != capitalized_first_word:
    print ("Sentence does not start with a capital letter.")
    # Replace the first word in the sentence with the capitalized word:
    sentence = sentence.replace(first_word, capitalized_first_word)

# Check if the sentence does not end with a punctuation mark, -1 is the index of the last character in the sentence:
if not sentence[-1] in string.punctuation:
    print("Sentence does not end with punctuation.")
    # Add punctuation to the end of the sentence:
    sentence += '.'

# Print the sentence:
print(sentence)

查看string indexing以获得更多详细信息。