This problem took me way too long time to solve. All because of a mistake I made caused by naming my dictionaries too similarly. When I thought I was adding "hundred_and_text", I really was adding "hundreds_text". What made it really annoying is that I made my code output all the thousand numbers in words, which worked great. For troubleshooting I copied the output into excel and counted the letters in the cells which gave the correct answer, but when I used my code for counting, I missed ~300 letters. Next time I'm planning on doing something like this, I'm going to give dictionaries more distinguishable names.
In this program I also worked in multiple files, i.e. I had my dictionaries in a file called "dicts.py" and my main program in "main.py" (mostly because I could).
Dictionary code:
singulars_text = { # 36 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine',} ten_to_nineteen_text = { # 70 1: 'ten', 2: 'eleven', 3: 'twelve', 4: 'thirteen', 5: 'fourteen', 6: 'fifteen', 7: 'sixteen', 8: 'seventeen', 9: 'eighteen', 10: 'nineteen'} tens_text = { # 46 2: 'twenty', 3: 'thirty', 4: 'forty', 5: 'fifty', 6: 'sixty', 7: 'seventy', 8: 'eighty', 9: 'ninety'} hundreds_text = { # 107 1: 'onehundred', # onehundred 2: 'twohundred', # twohundred 3: 'threehundred', # threehundred 4: 'fourhundred', 5: 'fivehundred', 6: 'sixhundred', 7: 'sevenhundred', 8: 'eighthundred', # eighthundred 9: 'ninehundred', # ninehundred} hundred_and_text = { # 126 1: 'onehundredand', # onehundredand 2: 'twohundredand', # twohundredand 3: 'threehundredand', # threehundredand 4: 'fourhundredand', # fourhundredand 5: 'fivehundredand', 6: 'sixhundredand', 7: 'sevenhundredand', 8: 'eighthundredand', 9: 'ninehundredand',}
Program code:
import dicts def number_counter_text(): tot = 0 for k, v in dicts.singulars_text.items(): # print(dicts.singulars_text[k]) tot += len(dicts.singulars_text[k]) for k, v in dicts.ten_to_nineteen_text.items(): # print(dicts.ten_to_nineteen_text[k]) tot += len(dicts.ten_to_nineteen_text[k]) for k, v in dicts.tens_text.items(): # print(dicts.tens_text[k]) tot += len(dicts.tens_text[k]) for a, b in dicts.singulars_text.items(): # print(dicts.tens_text[k], end="") # print(dicts.singulars_text[a]) tot += len(dicts.tens_text[k]) + len(dicts.singulars_text[a]) for k, v in dicts.hundred_and_text.items(): # print(dicts.hundreds_text[k]) tot += len(dicts.hundreds_text[k]) for x, y in dicts.hundreds_text.items(): # print(dicts.hundred_and_text[k], end="") # print(dicts.singulars_text[x]) tot += len(dicts.hundred_and_text[k]) + len(dicts.singulars_text[x]) for a, b in dicts.ten_to_nineteen_text.items(): # print(dicts.hundred_and_text[k], end="") # print(dicts.ten_to_nineteen_text[a]) tot += len(dicts.hundred_and_text[k]) + len(dicts.ten_to_nineteen_text[a]) for c, d in dicts.tens_text.items(): # print(dicts.hundred_and_text[k], end="") # print(dicts.tens_text[c]) tot += len(dicts.hundred_and_text[k]) + len(dicts.tens_text[c]) for e, f in dicts.singulars_text.items(): # print(dicts.hundred_and_text[k], end="") # print(dicts.tens_text[c], end="") # print(dicts.singulars_text[e]) tot += len(dicts.hundred_and_text[k]) + len(dicts.tens_text[c]) + len(dicts.singulars_text[e]) # print("onethousand") tot += len("onethousand") print("----- result -----") print(tot) pass number_counter_text()
/Ludvig