我必须为类创建一个脚本,该脚本接受用户输入编号,脚本需要输出该编号的书面形式。这需要使用递归来完成。当我运行我在下面复制的脚本时,它不起作用,我不确定问题是什么。它还需要使用相同的列表作为上层数字,如数万及以上。我对如何完成所有这些工作毫无头绪。为了使这个脚本有效,我需要修复什么?
number = input ('Please enter an integer ')
def name(number):
list1 = ["zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
list2 = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
list3 = ['', "", 'one-hundred', 'two-hundred', 'three-hundred', 'four-hundred', 'five-hundred' , 'six-hundred', 'seven-hundred', 'eight-hundred', 'nine-hundred']
if number < 20:
return list1[number]
if number < 100:
return list3[list2[number]]
hundreds_digit = number * 10
tens_digit = number / 10
ones_digit = number % 10
if ones_digit == 0:
return list2[tens_digit]
else:
return list2[tens_digit] + "-" + list1[ones_digit]
if tens-digit == 0:
return list3[hundreds_digit]
else:
return list3[hundreds_digit] + '-' + list2[tens_digit] + "-" + list1[ones_digit]
################
# Main Program #
################
name(number)
答案 0 :(得分:0)
这是我到目前为止所得到的,但由于输入111
时给予one-hundred ten one
,因此可以改进。
def hundreds(num):
obj = {1:'one-hundred', 2: 'two-hundred', 3:'three-hundred', 4:'four-hundred', 5:'five-hundred', 6:'six-hundred', 7:'seven-hundred', 8:'eight-hundred', 9:'nine-hundred'}
return obj[int(num)]
def tens(num):
obj = {1:"ten", 2:"twenty", 3:"thirty", 4:"forty", 5:"fifty", 6:"sixty", 7:"seventy",8:"eighty", 9:"ninety"}
return obj[int(num)]
def ones(num):
obj = {1:"one", 2:"two", 3:"three", 4:"four", 5:"five", 6:"six", 7:"seven", 8:"eight", 9:"nine"}
return obj[int(num)]
def written_form(num):
if len(str(num)) <= 0:
return False
else:
if len(str(num)) == 3:
print hundreds(str(num)[0]),
try:
num = int(''.join(list(str(num))[1:]))
except ValueError:
return
written_form(num)
elif len(str(num)) == 2:
print tens(str(num)[0]),
try:
num = int(''.join(list(str(num))[1:]))
except ValueError:
return
written_form(num)
elif len(str(num)) == 1:
print ones(str(num)[0]),
try:
num = int(''.join(list(str(num))[1:]))
except ValueError:
return
written_form(num)
def main():
var = int(raw_input('Enter your number: '))
written_form(var)
我有三个函数可以通过使用字典来获取数字的数百,数十和1:
>>> def hundreds(num):
... obj = {1:'one-hundred', 2: 'two-hundred', 3:'three-hundred', 4:'four-hundred', 5:'five-hundred', 6:'six-hundred', 7:'seven-hundred', 8:'eight-hundred', 9:'nine-hundred'}
... return obj[int(num)]
...
>>> hundreds(6)
'six-hundred'
>>> hundreds(4)
'four-hundred'
>>>
在&#39; main&#39;函数( written_form()
),如果num
的长度小于或等于0,我们就会破坏函数。如果不是,那么如果长度是3,我们打印第一个数字的数百,然后从整数中删除第一个数字,我们继续从那里开始。
该程序运行如下:
bash-3.2$ python written_form.py
Enter your number: 789
seven-hundred eighty nine
bash-3.2$ python written_form.py
Enter your number: 156
one-hundred fifty six
bash-3.2$ python written_form.py
Enter your number: 999
nine-hundred ninety nine
bash-3.2$ python written_form.py
Enter your number: 631
six-hundred thirty one
bash-3.2$
希望这有帮助!