0

This is my code and it is getting the syntax error "Invalid Character in Identifier". I can't figure out how to resolve this. I would appreciate any help available.

    def number_to_words(n):
      if n == 0:
        return "zero"
  ​
      unit = ("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
      tens = ("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
      teen = ("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 
             "eighteen", "nineteen")
      h, t, u = ("", "", "")
  ​
      if n//100:
        h = unit[n//100] + " hundred"
        n = n%100
    ​
      if n >= 20:
        t = tens[n//10]
        n = n%10
      
      elif n >= 10:
        t = teen[n-10]
        n = 0

      u = unit[n]

      return " ".join(filter(None,[h,t,u]))

    number_to_words()
AirSak2000
  • 1
  • 1
  • 1

1 Answers1

0

You have zero-width spaces in the example text, possible even more non-standard characters in the original code (before posting here). This is likely due to using an editor like Word or WordPad for writing your code.

Use a suitable text editor like Notepad++ or UltraEdit or a proper IDE like PyCharm or VSCode to write your code. It has many benefits, one of them avoiding these problems.

Here's the same code with special characters removed and spacing adjusted and it works OK:

def number_to_words(n):
    if n == 0:
        return "zero"

    unit = ("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
    tens = ("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
    teen = ("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
            "eighteen", "nineteen")
    h, t, u = ("", "", "")

    if n // 100:
        h = unit[n // 100] + " hundred"
        n = n % 100

    if n >= 20:
        t = tens[n // 10]
        n = n % 10

    elif n >= 10:
        t = teen[n - 10]
        n = 0

    u = unit[n]

    return " ".join(filter(None, [h, t, u]))


print(number_to_words(10))
Grismar
  • 27,561
  • 4
  • 31
  • 54