這段代碼令我瘋狂。我正在嘗試打印我正在編寫的程序的幫助列表。所以我定義了一個字典,其中的鍵是用戶可能想要澄清的詞,值是詞的描述。然後我使用一個for... in...
循環打印全部。把它簡單:格式化運算符修飾符在Python 3腳本中不起作用
ERROR = '\x1B[1;31m ERROR!! \x1B[0m'
WARNING = '\x1B[1;33m WARNING! \x1B[0m'
SUCCESS = '\x1B[1;32m Operation successful! \x1B[0m'
ABORTED = '\x1B[1;33m Operation aborted! \x1B[0m'
help_descriptions = {'\x1B[34m NUMBERS \x1B[0m':'are the options you can take.',\
ERROR:'means you ran into and error and the program can\'t go on.',\
WARNING:'means that the data you entered might cause problems.',\
SUCCESS:'means that no run-time errors where encoutered.',\
ABORTED:'means you aborted a previous option, deleting\n the data associated.'}
def HelpMe():
print(70 * '~')
print(' HELP')
print(70 * '~')
for key in help_descriptions.keys():
print('%10s %s' % (key, help_descriptions.get(key)))
print(70 * '~')
不工作的唯一的事情就是%10s
令牌。我的意思是,它確實打印了鍵的值,但如果需要,它不會放置額外的空格。我試過在互動部分運行這段代碼
print('%10s' % 'foo')
並且輸出是正確的。
您有沒有想過如何使其工作?
附加信息:我運行Ubuntu的11.04 Linux機器上運行的Python 3。此代碼是我編寫的用於存儲一些靜態文本或打印文本的簡單函數的自定義模塊的一部分。所以我在主應用程序中導入這個模塊,它不是獨立的。
預先感謝您。
如果您使用的是Python 3,請刪除'%'運算符並使用'str.format' – JBernardo
您是對的,我仍然試圖正確地安排python 2和python 3函數。 :P – mmassaro