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)
根据描述,你可能想得太多了:它只涉及一个句子,然后你只需确保第一个字母大写,结尾有标点符号:
它既可以被扩展(比如
.strip()
-ing空白,只需将其添加到input
-行),也可以缩短(第一个if
可以删除,因为对已经大写的东西调用.upper()
没有什么错)。但是,由于必须打印错误,if
必须保留在这个特定的情况下。在就这么做吧:
您的代码没有正确缩进,这就是在执行代码时导致缩进错误的原因。由于“lambda”的拼写不正确,lambda过滤器也会显示语法错误。如果else语句也未对齐,请注意如何缩进代码。在
这里有一个更简单的替代方案:
有关更多详细信息,请查看string indexing。在
相关问题 更多 >
编程相关推荐