Newbie Question
Fredrik Lundh
fredrik at pythonware.com
Tue Sep 21 08:29:28 EDT 1999
Dinesh Silva <ds297 at ecs.soton.ac.uk> wrote:
> I have a main function with the lines:-
>
> q1 = raw_input('Enter a number')
> cont(q1) # Cont is another function.
>
> Cont is a function which returns a number depending depending on the
> user input.
> However it doesn't work because I think that q1 is a string not an
> integer.
as you suspect, raw_input() returns
strings, not integers.
> Can someone tell me if this is true and if so, how to convert
> the string into an integer.
you can use "input()" instead -- it attempts
to translate the input to a Python value. in
this way, the user can enter integers,
floating point values, expressions, and
virtually anything else that is a valid
Python expression...
q1 = input('Enter a number')
cont(q1) # Cont is another function.
...or you can use the "int()" function to
convert strings to integer:
q1 = raw_input('Enter a number')
cont(int(q1)) # Cont is another function.
> I've tried the atoi function but keep getting
> errors.
hint: given that thousands of people will
read your message, don't just say that you
get errors, or that something don't work.
instead, take some time to tell us *what*
error you get. like in this case:
if you got a NameError, you forgot to
import the string module.
if you got a ValueError, you passed
bogus strings to string.atoi.
if you got TypeError, you passed some-
thing that was not a string to atoi.
> If the atoi function is the right one to use, could
> someone give an example on how to use it.
import string
print string.atoi(q1)
hope this helps!
</F>
More information about the Python-list
mailing list