Python Forum
Need help with this code and am stuck
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with this code and am stuck
#1
 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'
Reply
#2
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: "))
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have homework that I've made no effort to show any code that I'm stuck on Ronaldinho 1 2,347 May-30-2019, 07:18 PM
Last Post: nilamo
  Trying to teach myself how to code and I'm stuck on a question I found online... abushaa4 1 2,167 Dec-16-2018, 01:52 PM
Last Post: ichabod801
  stuck in a block of code fadi 1 2,876 Aug-26-2017, 02:26 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020