python - 检查大小写和句号的程序

标签 python regex string input capitalize

我是一名初学者程序员。我正在尝试编写一个程序,该程序会询问一个句子,然后检查两件事。 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/

相关文章:

python - Key=Val 使用正则表达式匹配

python - 如何将值映射到位?

python - 属性错误 : __enter__ from "with tf.Session as sess:"

python - 如何使用django向表中插入数据

bash 3.2.57 (macOS) 中的字符串连接

string - 在Elasticsearch上使用query_string在字符串字段中搜索数字

android - 字符串资源中的土耳其语字符

python - 使用 all() 比较字典中的值的多行 if 语句

c# - 查找指定函数的字符串文字参数

regex - 使用 RegEX 在 Notepad++ 中添加前缀和附加