输入0到100的一个随机数输出输入数字的英文用python
时间: 2024-05-08 16:16:52 浏览: 66
以下是Python代码实现:
```python
import random
# 生成随机数
num = random.randint(0, 100)
# 数字对应英文列表
num_dict = {
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"
}
# 判断数字大小并输出对应的英文
if num <= 20:
print(num_dict[num])
elif num < 100:
ten_digit = num // 10 * 10
one_digit = num % 10
if one_digit == 0:
print(num_dict[ten_digit])
else:
print(num_dict[ten_digit] + "-" + num_dict[one_digit])
else:
print("number out of range")
```
输出结果示例:
```
3
three
21
twenty-one
87
eighty-seven
101
number out of range
```
阅读全文