Python输入n,输出n个100到200内的随机数列表,从小到大逐行输出
时间: 2024-03-24 07:36:03 浏览: 20
好的,这是一个简单的Python程序,可以实现您的需求:
```python
import random
n = int(input("请输入一个整数n:"))
nums = []
for i in range(n):
nums.append(random.randint(100, 200))
nums.sort()
for num in nums:
print(num)
```
程序首先接收一个整数n,然后使用循环随机生成n个100到200内的整数,将它们存储在一个列表中。接下来,使用sort()方法对列表进行排序,并逐行输出排序后的列表元素。
相关问题
用python生成20个随机数按照从小到大的顺序输出
可以使用Python内置的random模块来生成随机数,再使用sorted函数排序输出。具体代码如下:
```python
import random
# 生成20个随机数,范围为1到100
numbers = [random.randint(1, 100) for _ in range(20)]
# 排序并输出结果
print(sorted(numbers))
```
其中,`random.randint(1, 100)` 表示生成一个1到100之间的随机整数,`range(20)`表示生成20个随机数。`sorted()` 函数用于排序,`print()`函数用于输出结果。
输入0到100的一个随机数输出输入数字的英文用python
以下是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
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)