[Tutor] How can i use both float and int?

Michael Dunn misha.dunn at gmail.com
Tue Feb 22 22:54:01 CET 2005


Hi .,,

An error will certainly occur (you should always try it and see).

You could do something like:

n = raw_input("Number1: ")
try: number1 = int(n)
except ValueError: number1 = float(n)

This will make number1 an integer if possible, and a float otherwise.
But (and wiser heads may correct me here) I'm not sure this is a good
idea. Couldn't you just use floats throughout, given that floats are
at least sometimes acceptable?
I'm just guessing what you need here, but if you really are trying to
avoid floats whereever possible, and if you're using python version
2.4, you might find that the decimal type is what you need. As I
understand it, it allows you to specify the precision of your number,
so 1 will be come out as to 1.00000000 and not 1.00000001 (or
whatever). I'm not using 2.4 myself yet, so ask the list if you need
examples or explanation.

Michael

On Tue, 22 Feb 2005 21:25:41 +0000, . , <administrata at hotmail.com> wrote:
> like...
> 
> number1 = raw_input(float int("Number1: ")
> 
> But, I think error will occur...
> 
> Cheers! :)
> 
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's FREE!
> http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list