[Tutor] Limit raw_input to hundredth decimal point
Christian Witts
cwitts at compuscan.co.za
Fri Jul 1 12:48:58 CEST 2011
On 2011/07/01 09:30 AM, Ryan Kirk wrote:
> Is there a way to limit raw_input to the hundredth decimal point?
>
> For example, I'd like to allow the user to enter 5.75 but not 5.756:
>
> dollars = raw_input("Please enter a dollar amount: $")
>
> Thanks!
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
If you're using Windows you can use the msvcrt module which has an
implementation of getch() which gets 1 character at a time. Then you
would just scan your input 1 character at a time and as soon as a the
`.` is used you can limit it to 2 further key-strokes before you "ban"
input and carry on in your process flow.
--
Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110701/60901bef/attachment.html>
More information about the Tutor
mailing list