[英]Pythons string formatting
我收到錯誤“SyntaxError:無效語法”,但我不確定為什么:
print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen %d",%( chisquare(fitElectrons, wertElectrons, errorE[0]), chisquare(fitMyons, wertMyons, errorE[0]), chisquare(fitTauons, wertTauons, errorE[0]), chisquare(fitHadrons, wertHadrons, errorE[0]) )
在%
或模數之前不需要逗號。 正確的語法: print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen %d" %( 1 ))
此外,% 運算符的右側沒有有效的 int 類型。 例如,它們似乎是: 1,3,5,2
這不能轉換為浮點數,因為它包含,
。
而是這樣使用它: print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen %d %d %d %d" %(1,3,5,2))
或者
print("ChiSquare zum Elektronen ist %d, zum Myonen ist %d, zum Tauonen ist %d und zum Hadronen ist %d" %(1,3,5,2))
不幸的是,建議的評論對我不起作用。 但是,以下行有效(參見TypeError:在字符串格式化 python 期間並非所有參數都已轉換以獲取更多詳細信息):
print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen '{0}, {1}, {2}, {3}'" .format( chisquare(fitElectrons, wertElectrons, errorE[0]), chisquare(fitMyons, wertMyons, errorE[0]), chisquare(fitTauons, wertTauons, errorE[0]), chisquare(fitHadrons, wertHadrons, errorE[0])))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.