纸张问题Python
时间: 2023-12-25 22:27:26 浏览: 26
以下是纸张问题的Python代码和解释:
```python
n = input() # 获取输入的数字
a = [1189, 841] # 初始化列表
for i in range(8):
a.append(int(a[i]/2)) # 根据规律添加列表项
print(a[int(n[-1])]) # 输出对应项
print(a[int(n[-1])+1]) # 输出下一项
```
这段代码的作用是根据输入的数字,输出对应的纸张大小。规律是每一项都是前一项的一半,初始值为[1189, 841]。例如,输入数字1,则输出841和594,因为列表中第1项是841,第2项是594。
相关问题
纸张尺寸 python
以下是三种Python操作纸张尺寸的例子:
1. 获取默认纸张大小
```python
from docx import Document
doc = Document("./test.docx")
default_section = doc.sections[0]
print(default_section.page_width.cm) # 输出:21.59
print(default_section.page_height.cm) # 输出:27.94
```
2. 自定义纸张大小
```python
from docx import Document
from docx.shared import Cm
doc = Document("./test.docx")
default_section = doc.sections[0]
# 修改宽度和高度,即纸张大小改为自定义
default_section.page_width = Cm(30)
default_section.page_height = Cm(20)
doc.save("./test.docx")
```
3. 构造一个纸张大小数列
```python
n = input()
a = [1189, 841]
for i in range(8):
a.append(int(a[i]/2))
print(a[int(n[-1])])
print(a[int(n[-1])+1])
```
python纸张对折
当一张厚度为1毫米的纸不断对折时,对折多少次后,厚度可以超过珠穆朗玛峰(8848m)的高度?这是一个经典的Python入门练习题,代码如下:
```
paper = 0.001 #设置纸的厚度为1毫米,换算后为0.001米
number = 0 #次数,初始为0
while paper <= 8848:
paper = paper * 2 #每次对折,厚度乘以2,次数加1
number += 1
print('第' + str(number) + '次的厚度是' + str(paper) + '米')
```
经过计算,需要对折29次才能让纸张的厚度超过珠穆朗玛峰的高度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)