python设置输出宽度_在python中以设置的宽度打印ASCII菱形

Yes, this is a homework task. But just please, if you're going to give me the code please tell me what you've done in detail. I am extremely new to this.

So the task is to print an ASCII diamond depending on what width the user inputs. I can do the first half of the diamond, just not the bottom half, for some reason I just cannot see how to do it.

Here's my code:

wid = int(input("Width: "))

i = 1

while i <= wid:

print(" " * (wid - i) + "* " * i)

i = i + 1

Which will output the following if wid = 5:

Width: 5

*

* *

* * *

* * * *

* * * * *

解决方案i=1

j=input("ENTER NO =")

l=0

for i in range(i,j-((j/2)-1),1):

print (' ' * ((j+1)/2-i)+'*' *(i*2-1))

l=(j/2+1)

while (i==l):

i=1

for i in range(i,j,1):

print (' ' *((i*2)-i)+'*' *(j-i*2))

if [i==j-1]:

l=raw_input('')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,可以使用openpyxl模块来设置Excel单元格的宽度和行高。 设置单元格宽度可以使用column_dimensions属性中的width属性: ```python from openpyxl import Workbook # 创建Workbook对象 wb = Workbook() # 获取第一个工作表 ws = wb.active # 设置第一列的宽度为20 ws.column_dimensions['A'].width = 20 # 保存Excel文件 wb.save('example.xlsx') ``` 设置行高可以使用row_dimensions属性中的height属性: ```python from openpyxl import Workbook # 创建Workbook对象 wb = Workbook() # 获取第一个工作表 ws = wb.active # 设置第一行的高度为30 ws.row_dimensions[1].height = 30 # 保存Excel文件 wb.save('example.xlsx') ``` 注意,设置宽度和高度都是以磅为单位,1磅=1/72英寸。 如果要设置列宽自适应,则可以使用worksheet模块中的column_dimensions属性中的auto_size属性: ```python from openpyxl import Workbook from openpyxl.utils import get_column_letter # 创建Workbook对象 wb = Workbook() # 获取第一个工作表 ws = wb.active # 写入数据 for i in range(1, 6): col_letter = get_column_letter(i) ws[col_letter + '1'] = 'Column ' + str(i) # 设置列宽自适应 for col in ws.columns: max_length = 0 column = col[0].column # 获取列名 for cell in col: try: if len(str(cell.value)) > max_length: max_length = len(cell.value) except: pass adjusted_width = (max_length + 2) * 1.2 ws.column_dimensions[column].width = adjusted_width # 保存Excel文件 wb.save('example.xlsx') ``` 这里使用了get_column_letter函数将列索引转换为列字母。然后遍历每一列,计算每一列的最大宽度,并根据最大宽度设置列宽。这里使用了一个简单的公式:(max_length + 2) * 1.2,其中2是为了留出一些空隙,1.2是一个调整因子,可以根据实际情况进行调整。 希望这些代码能帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值