int vs. float
Steve D'Aprano
steve+python at pearwood.info
Sat Feb 11 22:27:31 EST 2017
On Sat, 11 Feb 2017 07:24 pm, Marko Rauhamaa wrote:
> boB Stepp <robertvstepp at gmail.com>:
>
>> According to the OP's professor's challenge, the OP needs to recognize
>> an input of "4.0" as a float and "4" as an integer, and to respond
>> with an error message in the float case, or "decimal number" case as
>> the OP phrased it. Apparently only positive integers are acceptable
>> input; all other inputs should generate an appropriate error message
>> by input type.
>
> Haven't been following the discussion, but this should be simply:
>
> ast.literal_eval("...")
>
> For example:
>
> >>> ast.literal_eval("( 1.0, 3 )").__class__.__name__
> 'tuple'
In what way does returning a tuple match the requirement "return an int or a
float or generate an error message"?
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list