Search code examples
pythonregexstringinputcapitalize

Program that checks for capitalisation and full stop


I am a beginner programmer. I am trying to write a program that will ask for a sentence, then check for 2 things. 1) A capital letter at the start of the sentence and 2) a full stop at its end. I also want it to print out a sentence that'll tell the user whether or not their sentence is correct. For example:

Enter a sentence: python is hard.

Your sentence doesn't begin with a capital letter.

and

Enter a sentence: Python is hard

Your sentence has no full stop at the end.

and

Enter a sentence: python is hard

Your sentence doesn't start with a capital letter and has no full stop at the end.

and lastly;

Enter a sentence: Python is hard.

Your sentence is perfect.

However, i am stuck and all i have is this mess:

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.")

Any help would be greatly appreciated.


Solution

  • Try this:

    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.'