[Tutor] [Long]General Questions

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Aug 12 22:22:37 EDT 2003


> I've got a question concerning this prog: why have u written
> "radius=input()"rather than "radius=raw_input()"
> I thought we use the second when we ask something to the user

You are right, raw_input is preferred because it is less of a 
security risk. input effectively does the same as raw_input but 
then evaluates the string as a python expression and returns 
the result, thus:

v = raw_input('?')  # user enters 5 + 4
print v             # prints '5 + 4'
v = input('?')      # user enters 5 + 4
print v             # prints 9
######## DONT TRY THIS ONE FOLKS! ############
v = input('?')      # user enters import os; os.system("format C:")
...ooops!           

But if you trust your users input() saves a bit of conversion 
work for the programmer.

Also notice above that input() and raw_input() can both take a 
prompt string as a parameter thus avoiding the need for the 
initial print statements in Kirk's original post.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list