512189654 发表于 2022-3-5 20:31:24

python编程题

给一个数字0-100,输出数字的英文表达,如果不在0-100,就返回空

ba21 发表于 2022-3-5 20:41:20

def getChr(n):
    c = chr(n)
    if 'A'<=c<='Z' or 'a'<=c<='z':
      return c
    else:
      return ''


n = int(input('请输入一个整数(0-100):'))

print(getChr(n))

512189654 发表于 2022-3-5 20:42:31

ba21 发表于 2022-3-5 20:41


0-100不是ascii值,就是要返回one two three这种

wp231957 发表于 2022-3-5 20:50:15

512189654 发表于 2022-3-5 20:42
0-100不是ascii值,就是要返回one two three这种

那你得先造个列表或字典,把这些onetwothree啥的,预存里面

傻眼貓咪 发表于 2022-3-5 21:10:50

不知道是不是你要的:words = {
    0: 'zero', 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', 100: 'one hundred'
}

num = int(input('请输入一个整数(0 ~ 100)'))
if num <= 20 or num == 100 or not num%10:
    print(words)
else:
    a, b = divmod(num, 10)
    print(words + '-' + words)请输入一个整数(0 ~ 100)99
ninety-nine

请输入一个整数(0 ~ 100)26
twenty-six

请输入一个整数(0 ~ 100)72
seventy-two

512189654 发表于 2022-3-6 12:53:06

傻眼貓咪 发表于 2022-3-5 21:10
不知道是不是你要的:

是我要的!非常感谢!!
页: [1]
查看完整版本: python编程题