![](/img/trans.png)
[英]ValueError: unsupported format character '!' (0x21) at index 2235
[英]ValueError: unsupported format character ')' (0x29) at index 21
我從Automate The Boring Stuff With Python得到了代碼。
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s(levelname)s- %(message)s')
logging.debug('Start of program')
def factorial(n):
logging.debug('Start of factorial( %)' % (n))
total = 1
for i in range(n + 1):
total *= i
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.debug('End of factorial( %)' % (n))
return total
print (factorial(5))
logging.debug('End of program')
當我運行代碼時,出現以下錯誤:
2015-06-18 12:51:47,073 - DEBUG- 程序回溯開始(最近調用最后一次):文件“/home/raqeeb.alam/PycharmProjects/E_mail/sub_links.py”,第 14 行,打印階乘 (5 ) 文件“/home/raqeeb.alam/PycharmProjects/E_mail/sub_links.py”,第 6 行,在 factorial logging.debug('Start of factorial( %)' % (n)) ValueError: unsupported format character ')' ( 0x29) 在索引 21
對於這些行:
print (factorial(5))
logging.debug('End of program')
應該:
logging.debug('Start of factorial(%s)' % (n))
...
logging.debug('End of factorial(%s)' % (n))
順便說一句:括號在這里是多余的。 可能很簡單:
logging.debug('Start of factorial(%s)' % n)
...
logging.debug('End of factorial(%s)' % n)
並且' %(asctime)s(levelname)s- %(message)s'
應該是' %(asctime)s%(levelname)s- %(message)s'
您缺少%
符號:
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s%(levelname)s- %(message)s')
# ^ HERE
logging.debug('Start of factorial( %)' % (n))
logging.debug('End of factorial( %)' % (n))
這兩行應該是
logging.debug('Start of factorial(%s)' % (n))
logging.debug('End of factorial(%s)' % (n))
這是一個通常不受支持的錯誤。 嘗試這個:
看起來 Python 正在將 % 解釋為類似 printf 的格式字符。 嘗試使用 %%
方法:
logging.debug('Start of factorial( %%)' % (n))
.
.
.
logging.debug('End of factorial( %%)' % (n))
要獲得更干凈的代碼,請嘗試刪除 n 周圍的 ()。
方法:
logging.debug('Start of factorial( %%)' % n)
.
.
.
logging.debug('End of factorial( %%)' % n)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.