Thomas Jones
Thomas Jones

Reputation: 365

Python: Convert numbers to words

Hello everyone I'm trying to produce a code that will convert a number to a word in a given dictionary. But it seems to not print anything at all. no errors or anything. I tried many things to find the problem, still nothing.

When I enter 6 the program kicks back nothing.

It should output [six].

I thought it was a spacing problem but I dont think that is the case.

Here's what I have

import string


value = input("Enter a number 1 - 9 separted by commas: ")

def user_input(value):
    numbers = {}
    user_list = value.split(',')
    numbers = [(x.strip()) for x in user_list]
    return numbers
    print(numbers)

user_input(value)

numbers = user_input()

unit_number = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 
           5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}

def convert_n_to_w(numbers):
    i = len(str(numbers))

    if i == 0:
        return

    if i == 1:
        return unit_number[numbers]

    print(unit_number[numbers])

convert_n_to_w(numbers)

Can anyone please show me what I did doing wrong?

Update!!!!!!!

I added convert_n_to_w(numbers) and telling me

line 38, in <module> convert_n_to_w(numbers)

NameError: name 'numbers' is not defined

When I thought I defined it.

Upvotes: 0

Views: 10797

Answers (5)

Sauvik Biswas
Sauvik Biswas

Reputation: 1

def n2w(num):
    unit_number = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 
    5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}
    st=""
    while num>0 :
        a=num%10
        for i,j in unit_number.items():
            if i==a:
                st=unit_number[i]+" "+st
        num=num//10
        st=st.rstrip()
    return (st)

Upvotes: 0

Barath Kumar
Barath Kumar

Reputation: 21

#Dictionaries for reference
ones = {"1":"One","2":"Two","3":"Three","4":"Four","5":"Five","6":"Six", "7":"Seven","8":"Eight","9":"Nine"}
afterones = {"10":"Ten","11":"Eleven","12":"Twelve","13":"Thirteen","14":"Fourteen","15":"Fifteen","16":"Sixteen", "17":"Seventeen","18":"Eighteen","19":"Nineteen"}
tens = {"2":"Twenty","3":"Thirty","4":"Fourty","5":"Fifty","6":"Sixty", "7":"Seventy","8":"Eighty","9":"Ninety"}
grand={0:" Billion, ",1:" Million, ",2:" Thousand, ",3:""}

#Function converting number to words of 3 digit
def num_to_wrds(val):
    if val != "000":
        ans = ""
        if val[0] in ones:
            x = val
            ans = ans + ones[val[0]] + " Hundered and "
        if val[1:] in afterones:
            ans = ans + afterones[val[1:]] + " "
        elif val[1] in tens:
            ans = ans + tens[val[1]] + " "
        if val[2] in ones:
            ans = ans + ones[val[2]]
        return ans


num = input("Enter the number: ") 

# Paddiing with zeros
pad = 12-len(str(num))
numinwrds = "0"*pad + str(num)

final =""
numlis = [numinwrds[0:3],numinwrds[3:6],numinwrds[6:9],numinwrds[9:12]]

for key,grp in enumerate(numlis):

    if grp!="000":
        final = final + num_to_wrds(grp) + grand[key]

print final

This prints upto Values in Billions you can add more in grand dictionary it works perfect Note: be sure to put the highest value in decreasing order

Upvotes: 2

Yarkee
Yarkee

Reputation: 9394

There are several problems with your code. I would like to modify you code. The code below can work well but not pythonic.

value = input("Enter a number 1 - 9 separted by commas: ")

def user_input(value):
    if isinstance(value, tuple):
        return list(value)
    else:
        user_list = value.split(',')
        numbers = [(x.strip()) for x in user_list]
        return numbers


unit_number = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 
           5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}

def convert_n_to_w(numbers):
    for n in numbers:
        print(unit_number[int(n)])

numbers = user_input(value)

convert_n_to_w(numbers)

Upvotes: 2

Anthon
Anthon

Reputation: 76599

You return from user_input before printing the numbers. The print statement in that function is never reached.

Update:

And as @Joni indicates you are not calling the function convert_n_to_w at all.

So no print statement is ever executed.

Upvotes: 1

Joni
Joni

Reputation: 111249

The print statement, print(unit_number[numbers]), is in the convert_n_to_w function. You don't call that function at any point, so the print statement is not being run.

Either call the function, or take the print statement out of the function.

Upvotes: 3

Related Questions