python核心程式設計第六章練習6 8

2022-08-15 02:48:11 字數 3537 閱讀 5559

6-8.

列表。給出乙個整型值,返回代表該值得英文,比如輸入89會返回「eight-nine」。附加題:能夠返回符合英文語法規律的新式,比如輸入89會返回「eighty-nine」。本練習中的值假定在0~1000。

【答案】

**如下:

number = int(raw_input('please input a number between 1 to 1000: ... '))

units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

tens_units = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']

print 'the number you input is: '

if 0 <= number <= 9: print units[number]

elif 10 <= number <= 19: print tens_units[number - 10]

elif 20 <= number <= 99:

if number % 10 == 0: print tens[number / 10 - 2]

else: print '%s-%s' % (tens[number / 10 - 2], units[number % 10])

elif 100 <= number <= 999:

if number % 100 == 0: print '%s hundred' % (units[number / 100])

elif number % 10 == 0 and (number - 100 * (number / 100)) != 10: print '%s hundred and %s' % (units[number / 100], tens[(number - 100 * (number / 100))/10 - 2])

elif number % 10 == 0 and (number - 100 * (number / 100)) == 10: print '%s hundred and ten' % (units[number / 100])

elif number % 10 != 0 and 20 < (number - 100 * (number / 100))<= 99: print '%s hundred and %s-%s' % (units[number / 100], tens[(number - 100 * (number / 100)) / 10 - 2], units[number - 10 * (number / 10)])

elif number % 10 != 0 and 10 < (number - 100 * (number / 100))< 20: print '%s hundred and %s' % (units[number / 100], tens_units[number - 100 * (number / 100) - 10])

elif number % 10 != 0 and 0 < (number - 10 * (number / 10)) < 10: print '%s hundred and %s' % (units[number / 100], units[number - 10 * (number / 10)])

else: print 'ten hundred'

【執行結果】

please input a number between 1 to 1000: ... 309

the number you input is:

three hundred and nine

please input a number between 1 to 1000: ... 721

the number you input is:

seven hundred and twenty-one

看了一些網上的答案 發現沒有自己做的那麼簡單易懂,算了自己做吧。下面是自己做的:

#!/usr/bin/env python

number = int(raw_input('please input a number between 0 to 1000:'))

digit = ['zero','one','two','three','four','five','six','seven','eight','nine']

tendigit = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']

units = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']

if 0 <= number <= 9:

print 'translate into english is:',digit[number]

if 10 <= number <= 19:

print 'translate into english is:',tendigit[number-10]

if 20 <= number <= 99:

print 'translate into english is:',units[int(number/10)-2],'-',digit[number%10]

if 100 <= number <= 999:

ten = number%100

if 0 <= ten <= 9:

print 'translate into english is:',digit[int(number/100)],'hundred and',digit[ten]

elif 10 <= ten <= 19:

print 'translate into english is:',digit[int(number/100)],'hundred and',tendigit[ten-10]

else:

print 'translate into english is:',digit[int(number/100)],'hundred and',units[int(ten/10)-2],'-',digit[ten%10]

測試:

please input a number between 0 to 1000:907

translate into english is: nine hundred and seven

please input a number between 0 to 1000:917

translate into english is: nine hundred and seventeen

python核心程式設計第六章練習6 15

轉換。a 給出兩個可識別格式的日期,比如mm dd yy或者dd mm yy格式。計算出兩個日期之間的天數。b 給出乙個人的生日,計算此人從出生到現在的天數,包括所有的閏月。c 還是上面的例子,計算出此人下次過生日還有多少天。答案 a 如下 def date convert date input m...

python核心程式設計第六章練習6 13

6 13.字串.string模組包含三個函式,atoi atol 和atof 他們分別負責把字串轉換成整型 長整型和浮點型數字。從python 1.5起,python的內建函式int long float 也可以做同樣的事了,本文來,complex 函式可以把字串轉換成複數 然而1.5之前,這些轉換...

python 核心程式設計 第六章習題

6 6 建立乙個類似 string.strip 函式 方法一 低效方法 大量複製和生成子串物件 def str strip s while len s 2 if s 0 s s 1 else break while len s 2 if s 1 s s 1 else break if s or s ...