编写一个程序,提示用户输入一个senten

2024-09-29 21:51:19 发布

您现在位置:Python中文网/ 问答频道 /正文

Write a program that prompts for the user to input a sentence. Then check this sentence to make sure the first word of the sentence is capitalized and the sentence ends with a punctuation mark. If it is not properly written, fix the sentence, print the type of error, and print the fixed sentence.

我按照这个类提供的说明进行操作,并一直得到第四行代码的无效语法错误。想知道是否有人知道原因,并能告诉我如何修复它或另一种写这个程序的方法。在

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)

Tags: andofthetoforinputstringis
3条回答

根据描述,你可能想得太多了:它只涉及一个句子,然后你只需确保第一个字母大写,结尾有标点符号:

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()-ing空白,只需将其添加到input-行),也可以缩短(第一个if可以删除,因为对已经大写的东西调用.upper()没有什么错)。但是,由于必须打印错误,if必须保留在这个特定的情况下。在

就这么做吧:

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)

您的代码没有正确缩进,这就是在执行代码时导致缩进错误的原因。由于“lambda”的拼写不正确,lambda过滤器也会显示语法错误。如果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。在

相关问题 更多 >

    热门问题