Question: def charCount(s): c=0 for ch in s: c+=1 return c def letterCount(s): c=0 for ch in s: if ch.isalpha(): c+=1 return c def digitCount(s): c=0
def charCount(s):
c=0
for ch in s:
c+=1
return c
def letterCount(s):
c=0
for ch in s:
if ch.isalpha():
c+=1
return c
def digitCount(s):
c=0
for ch in s:
if ch.isdigit():
c+=1
return c
def spaceCount(s):
c=0
for ch in s:
if ch.isspace():
c+=1
return c
def UpperCase(s):
c=0
for ch in s:
if ch.isupper():
c+=1
return c
def LowerCase(s):
c=0
for ch in s:
if ch.islower():
c+=1
return c
def digits(s):
c=0
for ch in s:
if ch.isdigit():
c+=1
return c
def space(s):
c=0
for ch in s:
if ch.isspace():
c+=1
return c
def Word(s):
c=0 W = ['@','#','$','%','&','+','-','=','<','>','*','/']
for ch in s:
if ch in W:
c+=1
return c
def punctuation(s):
c=0 p = ['!', "~" ,"`" ,"^" ,"(", ")", "_" ,"{","}","[","]","|","\\",";",":",'"',"'",",",".","?"]
for ch in s:
if ch in p:
c+=1
return c
def main(): while input!="": sentence = input("Enter sentence (return to exit): ")
print("Statistics on your sentence:")
print(" Characters:",charCount(sentence))
print(" Letters:",letterCount(sentence))
print(" Upper-case:",UpperCase(sentence))
print(" Lower-case:",LowerCase(sentence))
print(" Digits:",digits(sentence))
print(" Spaces:",space(sentence))
print(" Word characters:",Word(sentence))
print(" punctuation:",punctuation(sentence))
main()
- Update main so that it repeatedly asks the user for a sentence and prints out the statistics. Exit when the input is an empty string, no characters.
- It makes sense to update the prompt to tell the user how to exit the program.
- If there is no initial input, that is, if the user presses Enter immediately, exit the program without printing any statistics.
- This shall use a loop. (So, don't include a second call to main to get this to happen again. That would be recursion
- when you press return it will end the program and print out Good-bye.
- Count the words in the sentence.
- Word count
-
For the purposes of this PA, a word is any contiguous combination of alpha-numeric characters. So 'alpha-numeric' is 2 words; the hyphen is not alpha-numeric. Hint: isalnum
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
