0

大家好,我正在尝试生成一个将数字转换为给定字典中的单词的代码。但它似乎根本不打印任何东西。没有错误或任何东西。我尝试了很多东西来找到问题,但仍然没有。

当我进入6程序时,什么都没有。

它应该输出[six].

我认为这是一个间距问题,但我不认为是这种情况。

这就是我所拥有的

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(数字)

谁能告诉我我做错了什么?

更新!!!!!!!

我添加convert_n_to_w(numbers) 并告诉我

line 38, in <module> convert_n_to_w(numbers)

NameError: name 'numbers' is not defined

当我以为我定义了它。

4

5 回答 5

3

print 语句,print(unit_number[numbers]),在convert_n_to_w函数中。您在任何时候都不会调用该函数,因此不会运行 print 语句。

要么调用函数,要么从函数中取出 print 语句。

于 2013-03-24T11:52:35.527 回答
2

您的代码有几个问题。我想修改你的代码。下面的代码可以很好地工作,但不是 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)
于 2013-03-24T12:00:08.830 回答
2
#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

这将打印到十亿的值,您可以在大字典中添加更多它完美地工作注意:确保将最高值按降序排列

于 2017-02-18T01:03:55.060 回答
1

returnuser_input之前打印的数字。该print函数中的语句永远不会到达。

更新:

正如@Joni 指出的那样,您根本没有调用该函数convert_n_to_w

所以print永远不会执行任何语句。

于 2013-03-24T11:52:36.103 回答
0
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)
于 2018-06-07T15:28:16.380 回答