Question
Minimal Level For the Minimal Level of the assignment, do the following: Read through the downloaded starting point code. Make sure you understand it. Add
Minimal Level
For the Minimal Level of the assignment, do the following:
- Read through the downloaded starting point code. Make sure you understand it.
- Add an initial comment block with your name and this assignment name and number
- Update the for loop so that it counts the characters rather than just print them out. We'll do a couple of them together.
Description of the variables
charCount This variable should hold the total number of characters within the input sentence.
letterCount This variable should hold the number of alphabetic characters within the input sentence. Hint 1: The string method isalpha returns True if the string consists of only alphabetic characters. Hint 2: You will need to look at each individual character in the string and see if that single character is alphabetic or not. If it is, you need to count it.
digitCount This variable should return the number of numeric characters within the input sentence. Hint: The string method isdigit returns True if the string consists of only numeric characters.
spaceCount This variable should return the number of white-space characters within the input sentence. Hint: The string method isspace returns True if the string consists of only white-space characters.
Counting characters
This one is really easy. Let's update the loop. Right now, it prints out the characters:
# loop through the sentence for char in sentence: # count the characters print(char)
Let's make the loop count the characters instead of printing them. We'll do this by adding one to the charCount variable.
# loop through the sentence for char in sentence: # count the characters charCount += 1
Notice that we have replaced the call to print with an update to the counter. This makes sense since we don't need to print out the individual letters.
Remember that the variable charCount has already been created at the top of main in the starting point code. If it hadn't, this would cause an error, because charCount += 1 needs to be able to get the current value of the charCount variable. [fodder: Why does this start with the word "remember"? Where have we seen this before?]
Counting spaces
Let's work through how we can count spaces. The place that we'll need to do more work in the for loop.
# loop through the sentence for char in sentence: # count the characters charCount += 1
Hint 2 above suggests that we use an if statement in the loop to check the character.
# loop through the sentence for char in sentence: # count the characters charCount += 1 # count the spaces if char == ' ': spaceCount += 1
We're just checking to the if the letter in char matches a space.
This will work for most cases. However, the Hint for spaceCount suggests a different way to approach this.
# loop through the sentence for char in sentence: # count the characters charCount += 1 # count the spaces if char.isspace(): spaceCount += 1
As the Hint tells us, the isspace method will return True if the string in the variable to the left of the dot (char, in this case) only has white-space characters within it. So, this will count not only space-bar spaces, but also tab characters and newline characters.
Since the value from isspace is already True or False, there is no need to compare the result of isspace with True. So, this is a better solution than the following:
# don't do this, this is considered bad programming practice if char.isspace() == True:
Not only is the check (== True) unnecessary, there are cases where it will give the wrong answer. [really hard fodder: Can you think of how or why this could be true?]
Counting Letters and Digits
Now, you need to add code to update letterCount and digitCount appropriately.
Sample run
The completed script should not echo the individual characters. It should print out the correct counts.
Notice, this doesn't count the punctuation.
Enter a sentence: Testing, 1, 2, 3. Statistics on your sentence: Characters: 17 Letters: 7 Digits: 3 Spaces: 3
Notice, this doesn't count the punctuation. We'll address that in at the Standard Level/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started