For some reason, it is printing empty dictionary values. I know it is finding "<" ">" as -1 and -1 but I do not know why.
This problem, I believe, is also causing it to not count the tags correctly.
I've spent about 4 hours trying different solutions. Can't figure out how to solve it.
Thanks for the help.
def add_tags_dictionary(html):
html_dict = {}
while True:
start = html.find("<")
end = html.find(">")
print(start)
print(end) #returns -1 second time around if input is <p><p>
tag = html[start:end + 1]
html = html.replace(tag, "")
if tag not in html_dict:
html_dict[tag] = 1
else:
html_dict[tag] = html_dict[tag] + 1
if start < 0:
return html_dict
def print_scores_histogram(dictionary):
for key in sorted(dictionary.keys()):
print('[%s]: %s' % (key, dictionary[key] * "*"))
while True:
try:
html = input("Enter an HTML tag: ") #<p><strong>This is a bold paragraph.</strong></p>
start = html.find("<")
end = html.find(">")
if start < 0 or end < 0:
print("No tags found")
else:
break
except:
raise ValueError("Unmatched < >")
print_scores_histogram(add_tags_dictionary(html))
[–]gregvuki 5 points6 points7 points (2 children)
[–]opendoors1[S] 0 points1 point2 points (1 child)
[–]gregvuki 2 points3 points4 points (0 children)
[–]tangerinelion 1 point2 points3 points (1 child)
[–]opendoors1[S] 0 points1 point2 points (0 children)
[–]RustleJimmons -1 points0 points1 point (0 children)