我是一名初学者程序员。我正在尝试编写一个程序,该程序会询问一个句子,然后检查两件事。 1) 句子开头大写字母,2) 句末句号。我还希望它打印出一个句子,告诉用户他们的句子是否正确。 例如:
输入一句话:Python很难。
您的句子不是以大写字母开头。
和
输入一句话:Python很难
你的句子结尾没有句号。
和
输入一句话:python很难
您的句子不是以大写字母开头,结尾也没有句号。
最后;
输入一个句子:Python 很难。
你的句子很完美。
但是,我被困住了,我所拥有的只是一团糟:
sentence = input("Sentence: ")
if sentence[0].isupper():
print("")
if (sentence[0].isupper()) != sentence:
print("Your sentence does not start with a capital letter.")
elif "." not in sentence:
print("Your sentence does not end with a full stop.")
else:
print("Your sentence is correctly formatted.")
任何帮助将不胜感激。
最佳答案
试试这个:
sentence = input('Sentence: ') # You should use raw_input if it is python 2.7
if not sentence[0].isupper() and sentence[-1] != '.': # You can check the last character using sentence[-1]
# both the conditions are not satisfied
print 'Your sentence does not start with a capital letter and has no full stop at the end.'
elif not sentence[0].isupper():
# sentence does not start with a capital letter
print 'Your sentence does not start with a capital letter.'
elif sentence[-1] != '.':
# sentence does not end with a full stop
print 'Your sentence does not end with a full stop.'
else:
# sentence is perfect
print 'Your sentence is perfect.'
关于python - 检查大小写和句号的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037630/