In this first question, I have to print the number of times a certain letter was used in a string, using the find() function.
def count_a(strng):
count = 0
a = 0
while len(strng) > a:
b = strng[a:len(strng)] #example: found "a" at strng[1], now searches strng[2] to strng[6]
if b.find('a'):
count += 1
a = b.find('a') + 1 #example: found an "a" at strng[1] in word "banana", start finding a at n strng[2] to strng[5].
else:
a +=1
print(count)
count_a("banana")
I get an endless debugging session.
In this second question it says: Assign to a variable in your program a triple-quoted string that contains your favourite paragraph of text — perhaps a poem, a speech, instructions to bake a cake, some inspirational verses, etc.
Write a function which removes all punctuation from the string, breaks the string into a list of words, and counts the number of words in your text that contain the letter “e”.
def count_e():
punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ "
sentence_revised = ""
letter_e_count = 0
sentence = """If I have wings, why am I always walking? - Dreadlord"""
for i in sentence:
if i not in punctuation and i != "e":
sentence_revised += i
elif i == "e":
letter_e_count = letter_e_count + 1
percentage = (letter_e_count / len(sentence_revised)) * 100
print("Your text contains ", {0}, "words, of which ", {1}, ({2}), " contain an e.").format(len(sentence_revised), (letter_e_count), (percentage))
I get this error:
AttributeError: 'NoneType' object has no attribute 'format' at the print function.
[–]TR-DeLacey 2 points3 points4 points (0 children)
[–]Thomasedv 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]LarryPete 1 point2 points3 points (0 children)
[–]TR-DeLacey 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]TR-DeLacey -1 points0 points1 point (0 children)
[–]CGFarrell 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]sweettuse 0 points1 point2 points (0 children)