我正在嘗試通過列印創建標尺,對于輸入值 5,它應該如下所示:

我試圖將我的代碼編號更改為符號,我的代碼是:
length = str(input("Enter the ruler length = "))
def ruler(string):
top = []
top_out = []
bottom = []
for i in range(length):
top.append((i 1)//10)
bottom.append((i 1)%10)
for i in range(length):
if ((i 1)//10) == 0:
top_out.append(" ")
elif (((i 1)//10) in list(sorted(set(top)))) and (((i 1)//10) not in top_out):
top_out.append(((i 1)//10))
else:
top_out.append(" ")
print (''.join(list(map(str, top_out))))
print (''.join(list(map(str,bottom))))
print (string)
如何糾正它以獲得適當的標尺輸出格式?
uj5u.com熱心網友回復:
像這樣的小功能可以降低標尺列印,
def print_ruler(n):
print('|....'*(n) '|')
print(''.join(f'{i:<5}' for i in range(n 1)))
執行:
In [1]: print_ruler(5)
|....|....|....|....|....|
0 1 2 3 4 5
In [2]: print_ruler(10)
|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10
In [3]: print_ruler(15)
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
對于兩位數,它不會到中心。
例如: For 12,|與數字對齊,1否則2不能進入中心12
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/516509.html
標籤:Python细绳
上一篇:如何翻轉字串中的偶數和奇數元素?
