# 菜鸟玩python的题目：

image.png

``````  pttn=r'\s'
result=re.sub(pttn,'',words)
``````

# 解题的代码下：

``````from  num2words import num2words
import re
words_number=0
for num in range(1,1002):
pttn=r'[\s\-]'
word=num2words(num)
words=re.sub(pttn,'',word)
words_number=words_number+len(words)
print(words_number)
``````

https://codereview.stackexchange.com/questions/39183/python-converter-number-to-english-project-euler-17

1.单词表

``````   NUMBER_WORDS = {
1 : "one",
2 : "two",
3 : "three",
4 : "four",
5 : "five",
6 : "six",
7 : "seven",
8 : "eight",
9 : "nine",
10 : "ten",
11 : "eleven",
12 : "twelve",
13 : "thirteen",
14 : "fourteen",
15 : "fifteen",
16 : "sixteen",
17 : "seventeen",
18 : "eighteen",
19 : "nineteen",
20 : "twenty",
30 : "thirty",
40 : "forty",
50 : "fifty",
60 : "sixty",
70 : "seventy",
80 : "eighty",
90 : "ninety"   }
``````

2.函数部份：

``````   def convert_number_to_words(num):
#Works up to 99,999
num = str(num)
analyze = 0
postfix = remainder = None
string = ""
if len(num) > 4:
analyze = int(num[0:2])
remainder = num[2:]
postfix = " thousand "
elif len(num) > 3:
analyze = int(num[0:1])
remainder = num[1:]
postfix = " thousand "
elif len(num) > 2:
analyze = int(num[0:1])
remainder = num[1:]
postfix = " hundred "
if int(remainder) > 0:
postfix += "and "
elif int(num) in NUMBER_WORDS:
analyze = int(num)
else:
analyze = int(num[0:1] + "0")
remainder = num[1:]
postfix = "-"
string = NUMBER_WORDS[analyze]
if postfix is not None:
string += postfix
if remainder is not None and int(remainder) > 0:
return string + convert_number_to_words(remainder)
else:
return string
``````

3.调用函数

``````   “用于测试此函数是否可行”
num=10
while int(num) >0:
if num =='q':
break
else:
result=convert_number_to_words(num)
print(result)
``````

image.png

`````` def convert_number_to_words(num):
#Works up to 99,999
num = str(num)
analyze = 0
postfix = remainder = None
string = ""
if len(num) > 4:
analyze = int(num[0:2])
remainder = num[2:]
postfix = " thousand "
elif len(num) > 3:
analyze = int(num[0:1])
remainder = num[1:]
postfix = " thousand "
if int(remainder[0])== 0 and int(remainder[1:]) > 0:
remainder= num[2:]
postfix +=" and "
if int(remainder[1]) == 0:
remainder = num[3]
else:
remainder=num[3]
elif len(num) > 2:
analyze = int(num[0:1])
remainder = num[1:]
postfix = " hundred "
if int(remainder) > 0:
postfix += "and "
elif int(num) in NUMBER_WORDS:
analyze = int(num)
else:
analyze = int(num[0:1] + "0")
remainder = num[1:]
postfix = "-"
string = NUMBER_WORDS[analyze]
if postfix is not None:
string += postfix
if remainder is not None and int(remainder) > 0:
return string + convert_number_to_words(remainder)
else:
return string

word_number=0
for number in range(1,1002):
result=convert_number_to_words(number)
move_blank=re.sub(r'\s|-','',result)
word_number=word_number+len(move_blank)
print(word_number)
``````

# 体会：

1、学习编程可以锻炼我们的思维能力，将思考的过程可视化
2、编程可以获得即时反馈，这一点对于学习新的技能很重要，如果没有反馈，要想持续地投入时间学习，有点难，而编程可以，边尝试，边获得结果
3、对问题的拆解能力，将一个目标拆解成自己可以解决的小问题，再组合，在编程过程中得到完美的体现
4、编程是一种集体协作的活动，你可以利用他人的作品，用在自己的程序内从而更快捷地解决问题，而你的作品也可以成为他人解决问题的桥梁