Hugo0206 Hugo0206 - 7 months ago 11
Python Question

I need help changing integers to words in Python 3.4.3

Example: 1 = one.

All I need to do is make a count go up to 5, but I am terrible at coding and am struggling. The code requirement is to count from the number input between 2-5 and count UP to five, but the last requirement is that the output must be WORDED numbers.

num = int(input('Enter a number (2-5): '))
count = 2
while count <= num:
if num > 5:
print('invalid.')
num = int(input('Enter a number (2-5): '))
print(count)
count = count + 1


My desired print would be:

Enter a number (2-5):
INPUT '5'
Two
Three
Four
Five


or

Enter a number (2-5):
INPUT '3'
Two
Three


CURRENTLY I am getting:

Enter a number (2-5):
INPUT '5'
2
3
4
5

Answer

Python doesn't really recognize 'words' as they relate to numbers. Words representing numbers is a very human thing.

What you need is a dictionary where the string version of the number is the key and the value is the corresponding word.

For example:

words_num_dict = {'1':'one', '2':'two', '3':'three', '4':'four', '5':'five'}
num = int(input('Enter a number (2-5): '))
count = 2
while count <= num:
    if num > 5:
        print('invalid.')
        num = int(input('Enter a number (2-5): '))
    print(words_num_dict[str(count)])
    count = count + 1

More info on using dictionaries here.