Chapter 9 Weekly Assignment


#1

It has taken hours to get this far and I am excited to get this far but I do not know how to use the .isalpha() function to get pass the code returning None. I need it to pass. Do I use the “e”.isalpha() function like an initializer or something along those lines? Do I create a function within a function (i.e. nest)? Not sure what to do next. Any and all help is appreciated.

This is the result(output) I get:
The text contains 5 alphabetic characters, of which 5 (100%) are ‘e’.
Test Failed: expected The text contains 5 alphabetic characters, of which 5 (100.0%) are ‘e’. but got None
The text contains 21 alphabetic characters, of which 7 (33.33333333333333%) are ‘e’.
Test Failed: expected The text contains 21 alphabetic characters, of which 7 (33.3333333333%) are ‘e’. but got None
The text contains 57 alphabetic characters, of which 0 (0%) are ‘e’.
Test Failed: expected The text contains 55 alphabetic characters, of which 0 (0.0%) are ‘e’. but got None
The text contains 5 alphabetic characters, of which 5 (100%) are ‘e’.
Test Failed: expected The text contains 5 alphabetic characters, of which 5 (100%) are ‘e’. but got None
The text contains 21 alphabetic characters, of which 7 (33.33333333333333%) are ‘e’.
Test Failed: expected The text contains 21 alphabetic characters, of which 7 (33.33333333333333%) are ‘e’. but got None
The text contains 57 alphabetic characters, of which 0 (0%) are ‘e’.
Test Failed: expected The text contains 55 alphabetic characters, of which 0 (0%) are ‘e’. but got None


#2

The isalpha function is boolean-- it returns True or False.

ie with string “samplestring”:

if the characters in the string are alphanumeric, then samplestring.isalpha() returns a value of “True”. If not, a value of “False” is returned.

Incidentally, I found this with a simple Google search. You should be willing to Google, as well, as a coder-- a lot of answers are out there in various documentations/on sites.


#3

You might try setting up some print() statements at places where you are expecting some kind of output or return from your program to see what it is actually doing. Logic errors are hard to find and can be maddening, but this is one way to track them down and fix them.

I say that because your program is outputting/returning the value None. Where is that coming from, and why?

Use isalpha() to control program flow. If it is true (as noted in another post, it is boolean–it only returns true or false), do something specific with your data value(s); if it is false, do something else. In this instance, if the character you are evaluating “isalpha”, what will you do with it? If is is not “isalpha”, what will you do differently?

After a code statement, add a print() line to see if what you are expecting is what you’re getting. Then you’ll have important information about what to do next. For example:

for char in charset:
    if char.isalpha():
        print(char)
    else:
        print("That's not an alphabetic character")

Try using print() in various places to see if you can find where None is coming from and then you’ll know where to focus your problem solving work.


#4

Thank you so much! This is very helpful.


#5

Thank you, yes, and I did do a google search but I’ve come to realize and know that it is okay to have a different learning style than others. I learn from the combination of asking, looking up things, reading, etc. No question is to dumb to ask right? So, I appreciate you taking the time to respond to my question! It was also helpful. :heart:


#6

@bmarkj: So I was able to pass the 3 of the 6 tests which is what I needed to do but when I submitted it, it gave me this error:

You should have returned this:
“The text contains 60 alphabetic characters, of which 7 (11.666666666666666%) are ‘e’.”

But you actually returned this:
“The text contains 63 alphabetic characters, of which 7 (11.11111111111111%) are ‘e’.”

I believe this is because the text in the program where we submit it is analyzing different text answers than what is posted in our assignment.

I believe it has something to do with one of these lines of my code:

a = len(text) - text.count(" “) - text.count(”!") - text.count("’") - text.count(",") - text.count(";") - text.count(",")
total = text.count(“e”) + text.count(“E”)

I have googled how to only count letters and not the spaces or special characters but I still need help understanding how not to count those. The above method works but it is hard coded for the specific text answers in the assignment so it would not pass anything else necessarily. I understand that.

Will you help me?


#7

Sure! Check your messages.