1

I get the error "SyntaxError: invalid syntax", though I am not really sure why:

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]) ) 
2
  • 2
    Drop the comma before "%" in ,%. The percent sign is used as an operator (like +, -, *, etc.). Commented Mar 28, 2020 at 7:27
  • Delete the ',' after format string, its "format string" % (agr1, arg2, arg3...), without the ',' Commented Mar 28, 2020 at 7:27

2 Answers 2

2

The comma is not required before the % or modulus. Correct syntax : print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen %d" %( 1 ))

Additionally, you do not have a valid int type on the right-hand side of the % operator. They seems be, for example : 1,3,5,2 This cannot be converted to float as this contains the ,.

Rather use it this way: print("ChiSquare Elektronen, Myonen, Tauonen und Hadronen %d %d %d %d" %(1,3,5,2))

or

print("ChiSquare zum Elektronen ist %d, zum Myonen ist %d, zum Tauonen ist %d und zum Hadronen ist %d" %(1,3,5,2))

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So if I understand you correctly, the %-operator should not be used for floats, right?
You can. For printing float type use %f instead of %d. FYI, %f simply prints the whole float value. And Ex: %6.2f prints the float such that the length of the number is 6 and the decimal part is just 2. Ex: for the number 13.45 applying %6.2f gives us 13.4500 for the number 123.456768 applying %6.2f gives us 123.45
Thanks again, I find it really interesting. When I use print("\nP-Werte Eektronen, Myonen, Tauonen und Hadronen: {0}, {1}, {2}, {3}".format(pvalElectrons, pvalMyons, pvalTauons, pvalHadrons)), I obtain "P-Werte Eektronen, Myonen, Tauonen und Hadronen: 0.8396741761875282, 0.9994747318207605, 0.9753723367002908, 0.9700954166525304", when I use print("\nP-Werte Eektronen, Myonen, Tauonen und Hadronen: %f %f %f %f" %(pvalElectrons, pvalMyons, pvalTauons, pvalHadrons)), I instead obtain 0.839674 0.999475 0.975372 0.970095. How could I get more digits as in the first case?
The %f prints 6 decimals as a default value. And hence the values are rounded off. But the {0} or {1} are simply place holders. So what ever are provided in the format() are just replaced. This could be any type, a string as well.
0

Unfortunately, the suggested comments do NOT work for me. However, the following line works (cf. TypeError: not all arguments converted during string formatting python for more details):

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]))) 

Comments