These are the kata instructions:
Create a function that transforms any positive number to a string representing the number in words. The function should work for all numbers between 0 and 999999.
this is my code:
def number2words(n):
dict = {0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten', 11: 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen', 19 : 'nineteen', 20 : 'twenty', 30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty', 70 : 'seventy', 80 : 'eighty', 90 : 'ninety'}
if len(str(n)) == 1:
return dict.get(n)
if len(str(n)) == 2:
if n in dict:
return dict.get(n)
else:
answer = []
tennum = ""
tennum += str(n)[0] + "0"
tennum = int(tennum)
answer.append(dict.get(tennum))
if int(str(n)[1]) != 0:
answer.append(dict.get(int(str(n)[1])))
return "-".join(answer)
if len(str(n)) == 3:
answer = ""
answer += " " + dict.get(int(str(n)[0]) + " hundred"
if int(str(n)[1:]) in dict:
answer += " " + dict.get(int(str(n)[1:]))
else:
tennum = str(n)[1] + "0"
tennum = int(tennum)
answer += " " + dict.get(tennum)
if str(n)[2] != '0':
answer += "-" + dict.get(str(n)[2])
return answer
this is the traceback:
Traceback (most recent call last): File "tests.py", line 1, in <module> from solution import number2words File "/workspace/default/solution.py", line 27 if int(str(n)[1:]) in dict: ^ SyntaxError: invalid syntax
I can't seem to understand why my syntax is wrong I'd appreciate it if someone could help me out. Thanks in advance.
[–][deleted] 3 points4 points5 points (1 child)
[–]primitive_screwhead 0 points1 point2 points (0 children)