[Tutor] err in integration

dn PyTutor at DancesWithMice.info
Sat May 1 16:39:21 EDT 2021


On 01/05/2021 19.19, Msd De wrote:
> Dear Sir,
> Thank you for your email
> 
> I tried this :
> print("The numerical result is {J1} (+-{err})".format(J1=J1, err=err))
> o/p is :
> The numerical result is 0.203920669888 (+-None)
> I would like it print number in place of +-None
> Thanks


Please re-read @Cameron's suggestion carefully. The simplified code uses
an f-string (Formatted string literals), so-named because the string
*commences* with the letter-f and can include object names, expressions,
etc, which are evaluated to produce a string-literal value - which you
wish to print but could equally-well be named and used elsewhere...

NB Python 3.6+

Web.Refs:
https://docs.python.org/release/3.6.0/reference/lexical_analysis.html#f-strings
https://www.python.org/dev/peps/pep-0498/



> On Sat, May 1, 2021 at 12:30 PM Cameron Simpson <cs at cskk.id.au> wrote:
> 
>> On 01May2021 12:07, Msd De <mmssdd1920 at gmail.com> wrote:
>>> Dear Sir,
>>> I would like to print the error in numerical calculation.
>>> I get this error
>>> *Traceback (most recent call last):*
>>>
>>> *  File "DFF_test.py", line 45, in <module>    print("The numerical result
>>> is {:J1} (+-{:err})".format(J1, err))ValueError: Invalid conversion
>>> specification*
>>
>> You don't want the colon before the parameter names in your format
>> string. Text after the colon is a format specifier (eg to print the
>> value in a particular way), rather than what you want, which is to name
>> the value you want to print.
>>
>> Try this:
>>
>>     print("The numerical result is {J1} (+-{err})".format(J1, err))
>>
>> Actually, that won't work: your providing _positional_ values to
>> .format(). Try this:
>>
>>     print("The numerical result is {J1} (+-{err})".format(J1=J1, err=err))
>>
>> Also, you can do this more simply with a "format string", thus:
>>
>>     print(f"The numerical result is {J1} (+-{err})")
>>
>> which pulls the parameter values straight our of your local variables.
>>
>> Cheers,
>> Cameron Simpson <cs at cskk.id.au>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

-- 
Regards,
=dn


More information about the Tutor mailing list