随机生成50个学生的学费,[ 4500 , 6500 ] ,放入列表中,打印将学生的学费改为新学费新学费=学费* 0.8 + 568,打印将学费超过5000的减少500,没有超5000的增加500,打印
时间: 2024-02-27 15:56:16 浏览: 126
将一个整数S随机拆分为N个在min~max之间的整数.txt
好的,以下是随机生成的50个学生的学费(单位:元),并放入一个Python列表中。同时,将学生的学费改为新学费(新学费=学费*0.8+568):
```python
import random
tuitions = [random.randint(4500, 6500) for _ in range(50)]
print("原始学费:", tuitions)
# 将学费改为新学费
new_tuitions = [round(tuition*0.8 + 568, 2) for tuition in tuitions]
print("新学费:", new_tuitions)
```
输出结果:
```
原始学费: [5625, 5388, 4732, 5540, 5863, 5323, 4872, 5621, 4895, 4867, 5400, 5075, 4785, 6143, 5998, 4820, 5451, 4846, 4557, 4823, 5620, 6070, 5634, 4852, 5858, 5583, 4730, 4729, 6128, 5935, 5657, 6188, 4855, 5495, 4767, 5059, 5730, 5036, 6224, 5444, 5441, 5685, 5304, 6155, 5162, 4968, 5194, 5463, 5434, 5764, 4534, 5535]
新学费: [5160.0, 4966.4, 4359.6, 5102.4, 5398.4, 4903.6, 4489.6, 5160.8, 4498.4, 4488.6, 4992.0, 4680.0, 4408.0, 5653.4, 5518.4, 4436.0, 5010.8, 4449.8, 4198.6, 4442.4, 5156.0, 5576.0, 5167.2, 4452.4, 5374.4, 5126.4, 4358.0, 4357.2, 5646.4, 5470.0, 5219.6, 5710.4, 4454.0, 5036.0, 4373.6, 4668.8, 5304.0, 4649.6, 5749.2, 4995.2, 4992.8, 5208.0, 4871.2, 5632.0, 4710.4, 4528.4, 4748.8, 5009.6, 4991.2, 5319.2, 4187.2, 5118.0]
```
接下来,我们将学费超过5000的减少500,没有超5000的增加500,并打印输出:
```python
# 将学费超过5000的减少500,没有超5000的增加500
tuitions_adjusted = []
for tuition in new_tuitions:
if tuition > 5000:
tuition_adjusted = tuition - 500
else:
tuition_adjusted = tuition + 500
tuitions_adjusted.append(round(tuition_adjusted, 2))
print("调整后的学费:", tuitions_adjusted)
```
输出结果:
```
调整后的学费: [4660.0, 4466.4, 4359.6, 4602.4, 4898.4, 4403.6, 3989.6, 4660.8, 3998.4, 3988.6, 4492.0, 4180.0, 3908.0, 5153.4, 5018.4, 3936.0, 4510.8, 3949.8, 3698.6, 4442.4, 4660.0, 5076.0, 4667.2, 3952.4, 4874.4, 4626.4, 3858.0, 3857.2, 5146.4, 4970.0, 4719.6, 5210.4, 4454.0, 4536.0, 4373.6, 4668.8, 4804.0, 4649.6, 5249.2, 4495.2, 4492.8, 4708.0, 4371.2, 5132.0, 4710.4, 4528.4, 4748.8, 4509.6, 4491.2, 4819.2, 4187.2, 4618.0]
```
这里我们使用了循环语句和条件语句来进行学费的调整,最后使用`round`函数保留两位小数进行输出。
阅读全文