Dick Moores wrote:
> I'm baaaack!
>
> I kept getting ideas for what I (and some of you) thought was a 
> finished yen-USD.py. And some of the good advice I got was to move on 
> to other things. I did for a while, but I kept thinking up new 
> revisions. The script has more than doubled in length. I'd previously 
> posted v4 at <http://www.rcblue.com/Python/yen-USD-v3.txt>.
>
> Here's v10: <http://www.rcblue.com/Python/yen-USD-v10.txt>
>
> New functions:
> again()
> divide2StringDecimals()
> multiply2StringDecimals()
> roundNumber() -- replaced setPrecision()
> printVariablesNotChanging()
> formatNumber()
> removeCommasFromNumbers()
>
> rather radically revised function:
> again()  -- offers several more choices
>
> The most important change is that because I realized I wanted the 
> program to be a general solution and give accurate answers for even 
> very large amounts of Yen or USD, I decided to operate with (number) 
> strings only, except when necessary in getRate() and getAmount() to 
> error check user inputs. Those floats are not used in the 
> calculations of Yen or USD.
>
> The most difficult function for me to write was roundNumber(), which 
> of course couldn't rely on the use of the built-in round() or the 
> formatting of strings (see the line "format = "%." + str(precision) + 
> 'f'" in setPrecision() in v3).  Lack of experience with the slicing 
> of lists caused many headaches. I didn't succeed in debugging until I 
> put in print statements wherever a value changes, and trying many 
> different integer strings and places (the arguments of 
> roundNumber()). A good lesson, I think.
>
>   
A few notes:
1. in your roundNumber function, you define a function incrementDigit.
I'm pretty sure that this function is destroyed and recreated every time 
you call the function roundNumber.
This applies to divide2stringdecimals and other functions as well.
Is this what you want?
2.  in your remove commas from numbers function, you could simplify it:
#from
    a = n.split(',')
    n = ''.join(a)
    return n
#to
    return ''.join(a.split(','))
personally I like this more.

> I hope some of the Tutors will take a look at the new functions, 
> especially roundNumber().
> Did I just reinvent the wheel?
>   
I'm pretty sure you could set up a Context in the decimal module that
will round and keep the precision where you specify.
So if this is true, I guess you did reinvent the wheel.
> Should it be broken up into more sub-functions (there's only one now)?
> It works, but is it Pythonic? Etc.
>   
I think you need to settle on a naming convention so your code's more 
readable.
Your variables are sometimes named aRandomVariable, with the start of 
each new word capitalized,
then in other places (like the variable 'slen' in numberCommas -> 
intCommas) they're not.
Also, it's easier to differentiate between functions and variables if 
they're named a different way.
Like a_variable_name = someFunction()
That's just my opinion, of course, and you can do whatever you want :)

Also, I've seen that you call the variable you store the split value in 
'splt'
Now while this approach may have its uses, it makes it more difficult to 
understand
what's going on in your code (for me).
If I were writing this software I might misread (or mistype!) split 
instead of splt, and not be able to figure out
what the problem was while I was debugging, whereas if the variable were 
named 'num_str' or something,
there's a smaller chance of confusion.

And lastly :)
props on the commenting.  They were helpful and informative.


> I'm also curious about multiply2StringDecimals() and divide2StringDecimals().
> Again, am I reinventing the wheel with these?
> Is there a simpler way to multiply and divide big decimals with precision?
>   
Well, the decimal module implements division and multiplication of 
decimal data type variables...
Eg.
#code:
context = decimal.Context() #replace this line with your customized 
context...
decimal.setcontext(context)
d= decimal.Decimal('6')
e = decimal.Decimal('3')
print d/e
#output:
2
Again, for the precision and such that you'll want for this,
look into making a custom context.
> Thanks in advance,
>
> Dick Moores
>   
Sure :D
-Luke

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to