Python Forum
Need help with this code and am stuck - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Need help with this code and am stuck (/thread-21225.html)



Need help with this code and am stuck - TheBoonester - Sep-19-2019

 nums = {1:"One", 2:"Two", 3:"Three" ,4:"Four", 5:"Five", 6:"Six", 7:"Seven", 8:"Eight",\
        9:"Nine", 0:"Zero", 10:"Ten", 11:"Eleven", 12:"Tweleve" , 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"}

num = input("Enter a number: ")

# to convert two digit number into words            
if 0 <= num < 100:
    a = num / 10
    b = num % 10
    if a == 1:
        print (nums[num])
    else:
        a *= 10
        print (nums[a], nums[b])
If anyone knows what I'm doing wrong please let me know :)

this is also the error I get when starting the program:

builtins.TypeError: '<=' not supported between instances of 'int' and 'str'


RE: Need help with this code and am stuck - j.crater - Sep-19-2019

input() function will return a string which you have entered. So even if you enter number 5, it will be stored as a string variable, not an int. That is why in this line:
if 0 <= num < 100:
you compare integers to a string (num). For this code to work, you need to first convert the number from string to integer type:
num = int(input("Enter a number: "))



RE: Need help with this code and am stuck - j.crater - Sep-19-2019

If you are allowed to use 3rd party modules, you could try num2words module.
And if this SO answer inspired you, I would advise you to first approach the task with your own creativity and test out different ideas.