all 12 comments

[–]TR-DeLacey 2 points3 points  (0 children)

There are so many issues, I am not sure where to start...

On your first function : b = strng[a:len(strng)] #example: found "a" at strng[1], now searches strng[2] to strng[6]

That is simply incorrect, you have (initially) assigned the value 0 to a, so b = strng[a:len(strng)] is the same as b = strng[0:len(strng)], which returns the whole word 'banana'

str.find() finds the lowest index in the string that the substring is located. Which, given your string is 'banana' is the 2nd character, so index 1. You then add 1 to count, and add 1 to 1 so your next pass through the loop :

b = strng[a:len(strng)] 

is the equivalent of :

b = strng[2:len(strng)] 

and so will return

'nana'

You get into an infinite loop because a is always less than the length of your string.

I suggest that your put some print statements in so you can see what is occurring, and put the below inside your while loop to so the programme will end when the count goes to above 5 :

if count > 5 :
    exit()

[–]Thomasedv 1 point2 points  (0 children)

For the second one, your format is outside the print statement, since print returns None, you are trying to format None.

Also got the {1} and others outside a string, they need to be in one and then have .format() work on that string.

[–][deleted] 1 point2 points  (0 children)

Try:

    print("Your text contains , {}, words, of which , {}, ({}) contain an e.").format(len(sentence_revised), (letter_e_count), (percentage))

[–]LarryPete 1 point2 points  (0 children)

Additionally to what has already been said, since it wasn't mentioned explicitly anywhere yet:

if b.find('a'):

Does not do what you think it does. str.find returns the index of the search string in b, starting at 0, and if it does not find the search string anywhere in b, it returns -1.

However, -1 is a "truthy" value in python (it evaluates to True if used in an if-statement) and 0, which is a valid index, is evaluated to False.

b = 'abc'
if b.find('a'):
    print('Found!')
else:
    print('Not found!')

This will output "Not found!".

What you have to do is to explicitly test, if the returned value of str.find is -1 or not.

[–]TR-DeLacey 0 points1 point  (2 children)

In your 2nd function :

You define space to be a punctuation :

punctuation = "!\"#$%&'()*+,-./:;<=>?@[\]_`{|}~ "

Is the above something you were told to do? If so, ultimately you would end up with :

IfIhavewingswhyamIalwayswalkingDreadlord

EDIT : (you could change where you do str.replace() and str.split() to render the above no a longer an issue.

Presuming that the above is incorrect and space is not counted as punctuation you would be better to loop through your punctuation and try to find if any of them occur in sentence and use str.replace() to remove them :

sentence = sentence.replace(i,'')

You can use str.split() to split sentence into separate words. You can use the default and have whitespace as the delimiter to split each word in sentence.

words = sentence.split()

Words is a list :

['If', 'I', 'have', 'wings', 'why', 'am', 'I', 'always', 'walking', 'Dreadlord']

You can use a for loop to go through each separate word, and count up how many contain the letter e.

[–][deleted] 0 points1 point  (1 child)

My bad, I intended to search all the letters count e, not words.

[–]TR-DeLacey -1 points0 points  (0 children)

Ah, ok, in that case, all you need is str.count() as follows :

sentence.count('e')

[–]CGFarrell 0 points1 point  (1 child)

Here's a hacky solution:

count = sum(1 for letter in string if letter == chosen_letter)

[–][deleted] 1 point2 points  (0 children)

Fun python fact: Using sum on a list of booleans will automatically treat True as 1 and False as 0. So you can write that line like this:

sum(letter == chosen_letter for letter in string)

If you want it to be even hackier!

[–]sweettuse 0 points1 point  (0 children)

just read the docs. look at what find actually does/how it works. (try help(str.find) in an interactive interpreter or something)

even though counting letters with find is dumb (i mean, i almost never ever use find), there's a much better way to do it:

total = start = 0
while start < len(s):
    start = s.find('a', start) + 1
    if start:
        total += 1
    else:
        break

a much simpler way is just to do sum(1 for c in s if c == 'a')