ehmatthes / intro_programming

A set of IPython notebooks and learning resources for an Introduction to Programming class, focusing on Python.
MIT License
396 stars 198 forks source link

Clarify raw_input() and input() in Python 2.7 #109

Closed ehmatthes closed 9 years ago

ehmatthes commented 9 years ago

Here it should say not to use input() in 2.7, and clarify that people should only use raw_input() in 2.7.

KMseven commented 9 years ago

we can use input on python 2.7 if input is used it stores only integers raw_input stores strings ex:number=input("enter a number") if you give number 5 to it then number is 5 number=raw_input("enter a number") if you give number 5 to it then number is 5(str(5) or string 5)

ehmatthes commented 9 years ago

It's insecure to use input() in 2.7, because input() tries to execute whatever the user enters. This can cause errors if you get unexpected input, and can result in insecure code being run as well. This is why input() was rewriteen in Python 3 to behave like raw_input() in 2.7, and input() was removed.

If you're running Python 2.7, use raw_input() and process the data as you need to afterward. If you're running Python 3, you have to use input(), and process the data as needed.

I'll try to update the site shortly to state this.

PS Thanks for replying! Please continue to make any suggestions you come up with. I'm planning to put significant time into this project again in the spring, once the project I'm currently working on is finished. I'll try to update any incorrect information, like this, in the next few days.