[Tutor] Some questions about my yen-USD.py
Luke Paireepinart
rabidpoobear at gmail.com
Thu Sep 7 11:27:43 CEST 2006
On 9/7/06, Dick Moores <rdm at rcblue.com> wrote:
>
> I've just finished a script for converting Yen to USD, and
> vice-versa. A simple, even silly thing to do (a friend asked me to
> write it for him--probably just to humor me), but I have tried to
> build in some bells and whistles. In doing so, some questions arose.
> If some of you Tutors could take look at my script, yen-USD.py, I'd
> greatly appreciate it. <http://www.rcblue.com/Python/yen-USD.txt>
>
> My questions:
>
> (1) Have I handled possible user-errors OK?
seems like it
(2) Is my roundingN() function OK? Is there a better way to write it?
> Will the line
I think that you could just use string formatting to do this...
say f is a float.
(Note I don't have access to a python interpreter right now so if this code
is incorrect it should at least
give you an idea of what I mean.)
so f is a float.
f = 0.023
s = str(f)
print f
should output '0.023'
and y = s.split('.')
so then you do...
f = int(y[0])
now f is the whole-number part of your number.
Now for the decimal part :D
f += float('.'+y[1][:roundingprecision-1])
if y[1][roundingprecision] >= 5:
f += float('.'.zfill(roundingprecision-1)+'1')
I hope that makes sense.
I'd be amazed if the code actually worked, but anyway, I think you see what
trickery I used to get this.
Someone else like Danny Yoo or Alan could probably awesome it more than I
can, though.
Hope that helps
n = round(float(n)*(10**rounding))/(10**rounding)
>
> get me into trouble with the flakiness of float(n)? In testing I
> didn't find any problems, but ..
the 'flakiness' of float is not going to get you into trouble. Your friend
won't get mad because he had to spend 1 more yen than he thought.
In some cases maybe it's a problem, but I don't think it will be here.
(3) Is there a better name for roundingN()? I don't like it, but
> can't think of a better one.
maybe round()?
or setprecision()? ;)
(4) I wanted to call closingMessage() in main(), but in some cases,
> if it's in main() it gets 2 successive calls. I can't figure out why.
> Would IDLE's debugger be of use here? (I've never used it before.) I
> haven't been able to find up-to-date IDLE help.
Didn't look at the code closely enough to tell, and I only have 5 hours to
sleep before my Music History class, so sorry.
I'll look at it again in the morning if no one else answers.
Thanks,
>
> Dick Moores
-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060907/53cda58d/attachment.html
More information about the Tutor
mailing list