鱼C论坛

 找回密码
 立即注册
查看: 8355|回复: 62

[已解决]Python:每日一题 332

[复制链接]
发表于 2020-2-15 21:15:25 | 显示全部楼层
本帖最后由 Windypper 于 2020-2-15 21:23 编辑
  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def input_num():
  4.     try:
  5.         num = int(input('Please input a positive integer: '))
  6.         if num < 1e15:
  7.             return num
  8.         else:
  9.             print('Integer must be less than 1E+15 and greater than 0')
  10.             input_num()
  11.     except ValueError:
  12.         print('Input Error\n\n')
  13.         input_num()


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


  21. def num_eng(num, loop=0):
  22.     num_text = ''
  23.     loop += 1
  24.     if num:
  25.         tens = num % 100  # extract tens digit and unit digit
  26.         if tens <= 20:  # the situation tens number less than 20
  27.             tens_text = cardinal[tens]
  28.         else:  # the situation tens number greater than 20
  29.             ten_digit = cardinal[tens - tens % 10]  # extract tens digit
  30.             unit_digit = cardinal[tens % 10]  # extract unit digit
  31.             if ten_digit and unit_digit:  # if both tens digit and unit digit are non-zero, add a hyphen between
  32.                 tens_text = f"{ten_digit}-{unit_digit}"
  33.             else:
  34.                 tens_text = f"{ten_digit}{unit_digit}"
  35.         if tens and loop > 1:  # if the number is greater than one thousand, add a space after unit digit
  36.             tens_text += ' '

  37.         hundreds = num // 100 % 10  # extract hundred digit
  38.         if not hundreds:  # if hundreds digit is zero, add nothing to the string
  39.             hundreds_text = ''
  40.         else:
  41.             hundreds_text = f'{cardinal[hundreds]} hundred '

  42.         # make string to be titlecased
  43.         tens_text = tens_text.title()
  44.         hundreds_text = hundreds_text.title()

  45.         # add 'and' before tens digit in two situations:
  46.         # 1. both tens digit and hundreds digits are non-zero
  47.         # 2. the last tens digit when the number is greater than one thousand
  48.         if tens and hundreds or num // 1000 and tens and loop == 1:
  49.             hundreds_text += 'and '
  50.         # add 'and' before hundreds digit if it is the last one
  51.         if num // 1000 and hundreds and not tens and loop == 1:
  52.             hundreds_text = 'and ' + hundreds_text

  53.         # recurse to higher magnitude
  54.         num_text = num_eng(num // 1000, loop)
  55.         return num_text + hundreds_text + tens_text + thousands[loop] + ' '

  56.     else:
  57.         if loop == 1:
  58.             num_text = 'Zero'
  59.         return num_text

  60. if __name__ == '__main__':
  61.     num = input_num()
  62.     print(num_eng(num))
复制代码


不是應該加上and嗎
試了下我這個好像沒問題

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
zltzlt + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-15 21:28:47 | 显示全部楼层
本帖最后由 Windypper 于 2020-2-15 21:34 编辑
zltzlt 发表于 2020-2-15 21:22
解答错误

输入:1000000

  1. # !/usr/bin/env python
  2. # -*- coding: utf-8 -*-

  3. def input_num():
  4.     try:
  5.         num = int(input('Please input a non-negative integer: '))
  6.         if 0 <= num < 1e15:
  7.             return num
  8.         else:
  9.             print('Integer must be non-negative and less than 1E+15')
  10.             input_num()
  11.     except ValueError:
  12.         print('Input Error\n\n')
  13.         input_num()


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


  21. def num_eng(num, loop=0):
  22.     num_text = ''
  23.     loop += 1
  24.     if num:
  25.         tens = num % 100  # extract tens digit and unit digit
  26.         if tens <= 20:  # the situation tens number less than 20
  27.             tens_text = cardinal[tens]
  28.         else:  # the situation tens number greater than 20
  29.             ten_digit = cardinal[tens - tens % 10]  # extract tens digit
  30.             unit_digit = cardinal[tens % 10]  # extract unit digit
  31.             if ten_digit and unit_digit:  # if both tens digit and unit digit are non-zero, add a hyphen between
  32.                 tens_text = f"{ten_digit}-{unit_digit}"
  33.             else:
  34.                 tens_text = f"{ten_digit}{unit_digit}"
  35.         if tens and loop > 1:  # if the number is greater than one thousand, add a space after unit digit
  36.             tens_text += ' '

  37.         hundreds = num // 100 % 10  # extract hundred digit
  38.         if not hundreds:  # if hundreds digit is zero, add nothing to the string
  39.             hundreds_text = ''
  40.         else:
  41.             hundreds_text = f'{cardinal[hundreds]} hundred '

  42.         # make string to be titlecased
  43.         tens_text = tens_text.title()
  44.         hundreds_text = hundreds_text.title()

  45.         # add 'and' before tens digit in two situations:
  46.         # 1. both tens digit and hundreds digits are non-zero
  47.         # 2. the last tens digit when the number is greater than one thousand
  48.         if tens and hundreds or num // 1000 and tens and loop == 1:
  49.             hundreds_text += 'and '
  50.         # add 'and' before hundreds digit if it is the last one
  51.         if num // 1000 and hundreds and not tens and loop == 1:
  52.             hundreds_text = 'and ' + hundreds_text

  53.         # recurse to higher magnitude
  54.         num_text = num_eng(num // 1000, loop)
  55.         if tens or hundreds:
  56.             return num_text + hundreds_text + tens_text + thousands[loop] + ' '
  57.         else:
  58.             return num_text

  59.     else:
  60.         if loop == 1:
  61.             num_text = 'Zero'
  62.         return num_text


  63. if __name__ == '__main__':
  64.     num = input_num()
  65.     print(num_eng(num))
复制代码


修改了下低級錯誤……

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-24 02:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表