gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:27:29 34 4
gpt4 key购买 nike

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

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com