Re: Working with decimals part 2

2014-08-27 Thread Seymore4Head
On Tue, 26 Aug 2014 10:46:56 +1000, alex23 wuwe...@gmail.com wrote:

On 26/08/2014 3:55 AM, Seymore4Head wrote:
 I changed the program just a little to give myself a little practice
 with number formats.  The main thing I wanted to do was make the
 decimal points line up.  The problem I am having is with the print
 (count)(payment)(balance) line.

While I don't want to discourage you from learning how to do it the long 
way, when you have a handle on it I highly recommend using a library for 
producing tabular data. tabulate is a very handy one, which supports 
lining up decimal points, amongst many other features:

https://pypi.python.org/pypi/tabulate

I personally find formatting text to be a pain and will always look for 
a better method.

Thanks for this link.

For some reason, I didn't see this message the day I started this
thread.  I am still rechecking questions I have asked.

I don't think I am ready for this yet, but hopefully soon.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with decimals part 2

2014-08-25 Thread Chris Angelico
On Tue, Aug 26, 2014 at 3:55 AM, Seymore4Head
Seymore4Head@hotmail.invalid wrote:
 For some reason, it is not working.  If I try to use row2 I get this
 error:
 http://i.imgur.com/FgeF9c9.jpg

Several meta-issues.

Firstly, your subject line talks about 'decimal' again. You're
actually working with floats; Python has a quite separate decimal
module, and it's not what you're doing here. It's confusing for those
of us who know that you're actually working in binary floating point
:)

Secondly: An image is a really bad way to capture an error message.
Instead of saying it is not working and uploading a screenshot to
imgur, just copy and paste the exception into the body of the email.

Thirdly: If you're working iteratively, keep the files as simple as
possible, and if you can do this as one-liners in Idle's interactive
mode, that's probably the easiest way to show us what's happening.
Don't bury all of them in together into a single program.

As to your actual issue... it's a little unclear, because the system's
interpretation of what you've written is completely different from
yours - due to one misplaced bracket.

 str(format(number), ',.2f'))

You want to format the number .2f, not str the formatted number .2f. I
think you can figure it out from there :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with decimals part 2

2014-08-25 Thread Mark Lawrence

On 25/08/2014 18:55, Seymore4Head wrote:

import sys
import math
def row1(number):
 return str(number).rjust(3)
def row2(number):
 return str(format(number) ',.2f'))
def row3(number):
 return '${:.2f}'.format(number)
def row4(number):
 return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

count = 0
payment = 0
borrowed = 100
rate = 6
term = 12
interest=borrowed*rate*.01   #(*1)
balance = borrowed + interest
print (Loan calculator)
print ()
print (Amount borrowed: , borrowed)
print (Interest rate: , rate)
print (Term: (months), term)
print ()
print (Amount borrowed: , borrowed)
print (Total interest paid: , interest)
print ()
print ()
print (Amount  Remaining)
print (Pymt#PaidBalance)
print (-   --   --)
while count =term:

 print ({} {} {}.format(row1(count),
row2(payment),row3(balance)))

 payment = (borrowed + interest)/term
 balance = balance - payment
 count = count + 1


I changed the program just a little to give myself a little practice
with number formats.  The main thing I wanted to do was make the
decimal points line up.  The problem I am having is with the print
(count)(payment)(balance) line.

I added 4 functions row1-4 for some practice in formatting.
Row4 is the old makeitmoney function.  I am not using it, but I am
keeping it in.

row2 is row4 with:
(math.floor(number * 100) / 100, ',.2f')
taken out leaving',.2f'
For some reason, it is not working.  If I try to use row2 I get this
error:
http://i.imgur.com/FgeF9c9.jpg

Most of my learning is trial and error.  Mostly error.  To try to get
the decimals to line up, I changed row3 from'${:.2f}' to '${:6.2f}'.
That makes the decimals line up, but it adds another problem.
http://i.imgur.com/1KsP3ga.jpg

If you change borrowed from 100 to 1000 the fix gets broken again.
So I changed the '${:6.2f}' to '${:8.2f}'
http://i.imgur.com/74C5sAx.jpg

That works until you change borrowed to 100
http://i.imgur.com/fCuwOXv.jpg
Is there a way to fix the decimal point to line up without having to
limit the whole digits?

BTW I changed row3 back to  '${:6.2f}' and used 1 000 000 000 for
borrowed  It doesn't lose any digits in the whole number column, but
it does skew the formatting.
http://i.imgur.com/Hjpkts4.jpg



The best approach to trial and error is to use the interactive prompt. 
You'll need something like it if you insist on mixing function calls 
that contain various types of string formatting with string formatting.


An alternative is to apply the KISS principle.

print ({x}{y}{z}.format(count, payment, balance)) is all you need, 
where x, y and z are the appropriate formatting options for each of 
count, payment and balance.  These options have already been pointed out 
to you.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: Working with decimals part 2

2014-08-25 Thread MRAB

On 2014-08-25 18:55, Seymore4Head wrote:

import sys
import math
def row1(number):
 return str(number).rjust(3)
def row2(number):
 return str(format(number) ',.2f'))


That line has to many ')'.

The result of 'format' is a string, so there's no need to use 'str'.


def row3(number):
 return '${:.2f}'.format(number)
def row4(number):
 return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))


Here, again, the result of 'format' is a string, so there's no need to 
use 'str'.


count = 0
payment = 0
borrowed = 100
rate = 6
term = 12
interest=borrowed*rate*.01   #(*1)
balance = borrowed + interest
print (Loan calculator)
print ()
print (Amount borrowed: , borrowed)
print (Interest rate: , rate)
print (Term: (months), term)
print ()
print (Amount borrowed: , borrowed)
print (Total interest paid: , interest)
print ()
print ()
print (Amount  Remaining)
print (Pymt#PaidBalance)
print (-   --   --)
while count =term:

 print ({} {} {}.format(row1(count),
row2(payment),row3(balance)))

 payment = (borrowed + interest)/term
 balance = balance - payment
 count = count + 1


I changed the program just a little to give myself a little practice
with number formats.  The main thing I wanted to do was make the
decimal points line up.  The problem I am having is with the print
(count)(payment)(balance) line.

I added 4 functions row1-4 for some practice in formatting.
Row4 is the old makeitmoney function.  I am not using it, but I am
keeping it in.

row2 is row4 with:
(math.floor(number * 100) / 100, ',.2f')
taken out leaving',.2f'
For some reason, it is not working.  If I try to use row2 I get this
error:
http://i.imgur.com/FgeF9c9.jpg

Most of my learning is trial and error.  Mostly error.  To try to get
the decimals to line up, I changed row3 from'${:.2f}' to '${:6.2f}'.
That makes the decimals line up, but it adds another problem.
http://i.imgur.com/1KsP3ga.jpg

If you change borrowed from 100 to 1000 the fix gets broken again.
So I changed the '${:6.2f}' to '${:8.2f}'
http://i.imgur.com/74C5sAx.jpg

That works until you change borrowed to 100
http://i.imgur.com/fCuwOXv.jpg
Is there a way to fix the decimal point to line up without having to
limit the whole digits?

BTW I changed row3 back to  '${:6.2f}' and used 1 000 000 000 for
borrowed  It doesn't lose any digits in the whole number column, but
it does skew the formatting.
http://i.imgur.com/Hjpkts4.jpg


There are 2 steps:

1. Format the amount as a string. This is best done in a function.

2. Right-justify the string. This could be done as part of the format
for the row.

--
https://mail.python.org/mailman/listinfo/python-list


Re: Working with decimals part 2

2014-08-25 Thread alex23

On 26/08/2014 3:55 AM, Seymore4Head wrote:

I changed the program just a little to give myself a little practice
with number formats.  The main thing I wanted to do was make the
decimal points line up.  The problem I am having is with the print
(count)(payment)(balance) line.


While I don't want to discourage you from learning how to do it the long 
way, when you have a handle on it I highly recommend using a library for 
producing tabular data. tabulate is a very handy one, which supports 
lining up decimal points, amongst many other features:


https://pypi.python.org/pypi/tabulate

I personally find formatting text to be a pain and will always look for 
a better method.


--
https://mail.python.org/mailman/listinfo/python-list