how do i specify a data type when accepting arguments in a function in Python? (i want to accept a float)
As far as I know, you don't. You can do some defensive programming (using type() to check etc), but giving a type for input preemptively, I don't think that it is possible. You declare def foo(x): and then, depending on the input, the interpreter(on the fly, I believe) knows whether it is an int, float or string and reacts accordingly. (int -> float conversion is implicit on Python also, so it works fine if the argument is 3 instead of 3.0 for example).
The input routines return a string. You convert/assign it. If you wrap all that in a try statement.... try: foo = float( raw_input( "Enter a number: " ) ) except: print "dummy!"
Thanks. yes, i ended up doing type checking. the equivilence of declaring "def foo(bar as int)" is possible in another language i know (VBA). just learning python now. :)
Join our real-time social learning platform and learn together with your friends!