Ad

Shorter number converter, Using a if instead of a switch.

Code
Diff
  • def converter(number):
        nums = ['zero', 'one','two','three','four','five','six','seven','eight','nine']
        if number < len(nums):
            return nums[int(number)]
        else:
            return number
    
    • def converter(number):
    • match number:
    • case 0:
    • return 'zero'
    • case 1:
    • return 'one'
    • case 2:
    • return 'two'
    • case 3:
    • return 'three'
    • case 4:
    • return 'four'
    • case 5:
    • return 'five'
    • case 6:
    • return 'six'
    • case 7:
    • return 'seven'
    • case 8:
    • return 'eight'
    • case 9:
    • return 'nine'
    • case _:
    • return number
    • nums = ['zero', 'one','two','three','four','five','six','seven','eight','nine']
    • if number < len(nums):
    • return nums[int(number)]
    • else:
    • return number