python - Formatting results of List in Python3 .4 -
i trying results of list calculations display in orderly fashion formatting print() function. have tried various methods site , python docs.
the closest can display in table format align output using .format() function, couple of lines still offset. understand because preceding results longer others, can't figure out way text align in columns.
when seek amend string formatting have "tuple index out of range" error. far aware i'm not using tuples?
this string trying print:
print('{:<10}'.format("richter scale value:"), '{:<10}'.format(rval),'{:^10}'.format ("joules:"),'{:^10}'.format(joules),'{:>10}'.format("tnt:"),'{:>10}'.format(tnt))
but output is:
richter scale value: 1 joules: 1995262.3149688789 tnt: 0.00047687913837688307 richter scale value: 5 joules: 1995262314968.8828 tnt: 476.87913837688404 richter scale value: 9.1 joules: 2.818382931264449e+18 tnt: 673609687.2046962 richter scale value: 9.2 joules: 3.981071705534953e+18 tnt: 951498973.5982201 richter scale value: 9.5 joules: 1.1220184543019653e+19 tnt: 2681688466.3048882
i can't figure out how lastly 3 'tnt' lines match up.
this code preceding print():
rval = 0 qlist=[1,5,9.1,9.2,9.5] qlist_len = len(qlist) in range (0,qlist_len): rval = qlist[i] joules = 10**((1.5*rval)+4.8) tnt = joules/(4.184*10**9)
any assistance gratefully received thanks.
you need explicitly format joules
values precision. have given minimum width, default format exceeds that:
print('richter scale value: {:<10.1f} joules: {:<10.8g} tnt: {:10.8g}'.format( rval, joules, tnt))
you may want tweak exact widths , how many decimals displayed. utilize f
instead of g
forcefulness displaying values without scientific e
notation, or utilize e
forcefulness scientific notation instead.
i combined 6 str.format()
calls one. there little point in formatting constant label strings.
python list string-formatting python-3.4
No comments:
Post a Comment