[Tutor] Checking that the input is float()-able

Amit Saha amitsaha.in at gmail.com
Sun Sep 22 23:27:25 CEST 2013


On Sun, Sep 22, 2013 at 11:28 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sun, Sep 22, 2013 at 10:18:23AM +1000, Amit Saha wrote:
>> Hi all,
>>
>> I want my program to report an error if anything other than a number
>> which I can then convert to float using float() is entered as input:
>>
>> # Python 3
>> try:
>>     a = float(input('Enter a number: '))
>>
>> except ValueError:
>>     print('Wrong input')
>> else:
>>     print('Right input')
>>
>>
>> This seems to do the job for me.
> [...]
>> I have two queries:
>>
>> - Is this the best way to do this?
>> - Is this is the easiest way to tell a beginner to do this? (Even
>> though it introduces try..except)
>
> Yes, and yes.
>
>


Thank you Joel and Steven.

Best,
Amit.


-- 
http://echorand.me


More information about the Tutor mailing list