大家好我正在尝试生成一个代码,将代码转换为给定字典中的单词。但似乎根本没有印刷任何东西。没有错误或任何东西。我尝试了很多东西来找到问题,但仍然没有。
当我输入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
当我以为我定义它时。
答案 0 :(得分:3)
打印语句print(unit_number[numbers])
位于convert_n_to_w
函数中。您不会在任何时候调用该函数,因此不会运行print语句。
调用函数,或从函数中取出print语句。
答案 1 :(得分: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)
答案 2 :(得分:1)
在打印数字之前,您return
来user_input
。永远不会达到该函数中的print
语句。
更新
正如@Joni所说,你根本没有调用函数convert_n_to_w
。
所以没有执行print
语句。
答案 3 :(得分:1)
#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
这打印到数十亿的价值,你可以在大字典中添加更多,它完美 注意:请务必按降序排列最高值
答案 4 :(得分: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)