2017-07-17 116 views
-3

我想此輸出的方式,其中在括號中的數是偶數格式化和與括號中的其他數字的直線排隊..下面所示輸入語句的格式化輸出

# AStE ....................(1) 
# AST......................(2) 
#ZASKW.....................(3) 
#gREEENN...................(4) 
# THESE ARE EXAMPLE NAMES WITH MORE LETTERS IN THEM. 



location = raw_input("\n \n THIS IS A LIST OF words with ID'S ASSIGNED TO THEM() \n ASTE (1)\n Ast (2) \n AS (3) \n ASTO (4) \n Bro (5) \n Cor (6) \n DUn (7) \n DUNWO (8) \n Ea (9) \n Eas (10) \n"" VI (11) \n Green (12) \n Mill (13) \n State (14) \n Ver (15) \n We (16) \n PLEASE ENTER THE ID # ") 
+4

看起來你要我們寫一些代碼給你。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。展示這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有),預期輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ](http://stackoverflow.com/tour)和[如何提問](http://stackoverflow.com/help/how-to-ask)。 –

+0

另外,如果括號中的數字爲10或更大,會發生什麼情況 - 這些數字是左對齊,右對齊還是其他?爲什麼'AStE'和'AST'線有一個領先的空間,但其他線沒有?你必須讓你的問題陳述更清楚。 –

+0

我的目標是嘗試排列圓括號,所以如果有一個位置名稱與很多字母,它會適當排隊。任何方式,我可以做到這一點。我正在嘗試手動執行它,但效率不高 – Mike

回答

0
實施例輸出

它應該做你想做的事。

commands = ["AStE", "AST", "ZASKW", "gREEENN"] 
max_columns = 30 

for index, commands in enumerate(commands): 
    stars_amount = max(max_columns - len(commands), 0) 
    row = "# {} {}({})".format(commands, "." * stars_amount, index + 1) 
    print row 
print "PLEASE ENTER THE ID:" 

輸出:

# AStE ..........................(1) 
# AST ...........................(2) 
# ZASKW .........................(3) 
# gREEENN .......................(4) 
PLEASE ENTER THE ID: 

基於@ason​​gtoruin評論另一種解決方案:

commands = ["AStE", "AST", "ZASKW", "gREEENN"] 

for index, commands in enumerate(commands): 
    row = "# {:.<30} ({})".format(commands, index + 1) 
    print row 
print "PLEASE ENTER THE ID:" 
+1

你可以用[字符串格式化](https://docs.python.org/2/library/string.html#format-specification-mini-language)更整齊地做到這一點。 – asongtoruin