Locked lesson.
About this lesson
Allow the user to interact with your program with raw_input and input.
Exercise files
Download this lesson’s related exercise files.
Getting User Input .docx59.3 KB Getting User Input - Solution.docx
59.6 KB
Quick reference
Getting User Input
There are several ways of getting user input.
When to use
How you get user import depends on the type of user input you plan to receive (strings, integers, etc).
Instructions
We'll look at raw_input(), input() and stdin.
raw_input() has been deprecated for python 3, but it seems to still work:
print(raw_input("Prompt: "))
raw_input converts whatever you type into a string.
To input numbers, use input() and write:
print(input("Enter a number: "))
To input a string in Python 3 the right way, import the sys module and then use stdin:
import sys
print("Prompt: ")
print(sys.stdin.readline())
Hints & tips
- raw_input() # Deprecated for python 3
- input() # input numbers
- stdin #import strings
- 00:04 In this video we're going to look at getting user input.
- 00:07 So up until now our program has just sort of run and output results.
- 00:11 And there's been no interaction between the user and the program.
- 00:14 And a lot of times, most of the time, you're going to want the user to be
- 00:17 able to interact, to be able to type things in, issue commands, do whatever.
- 00:20 So how do we do that with Python?
- 00:22 Well, it's a little bit tricky.
- 00:23 Because we will see right here, there's three main ways to do this.
- 00:27 There's probably other ways as well, but these are the main ways, raw_input input,
- 00:31 and this stdin thing.
- 00:32 The problem is raw_input was used in Python 2 and
- 00:35 it's since been deprecated for version 3 of Python.
- 00:38 But every time I try to use it in version 3 of Python, it seems to work.
- 00:42 So I don't know, I'm going to keep using it because it's super easy to use.
- 00:45 So what we do is let's go print to the screen.
- 00:49 And let's call raw_input, and it's a function.
- 00:53 So we have to pass it a parameter and
- 00:54 the parameter that we're going to pass it is the prompt.
- 00:58 What do we want to put on the screen?
- 00:59 So let's say what is your name, right?
- 01:03 So if we save this and run it, it says what is your name?
- 01:07 John, boom, it just spits back John.
- 01:09 So okay, that's neat.
- 01:10 But most of the time, you're not going to want to just spit back the result,
- 01:13 you're going to want to do something with it.
- 01:14 So let's put it into a variable.
- 01:17 If we save this and run it, hopefully you know by now that
- 01:20 nothing will happen because all we've done is put this input into this variable but
- 01:25 we didn't actually do anything with it.
- 01:26 So of course, you're going to want to print out your thing
- 01:29 if that's what you want to do with it later on.
- 01:31 So that's your name John, John, or
- 01:35 we could say Hello and then do our &s thing, like this, save it,
- 01:41 run it, John.
- 01:43 Hello John.
- 01:44 So that's kind of cool.
- 01:45 But like I said, that's been deprecated in version 3.
- 01:47 You're not supposed to use it anymore, but it still works as we can see.
- 01:50 So there's a couple of problems with this.
- 01:52 Let's run it again.
- 01:53 What is your name?
- 01:53 If we say 12, it says, Hello 12.
- 01:55 But if we go namer += 1, we try to add something to that number.
- 02:01 What's your name?
- 02:01 12, error.
- 02:02 The problem is when we use this_raw input,
- 02:05 it converts whatever you type into a string.
- 02:08 So if you want to use numbers, it won't work with numbers.
- 02:11 To use numbers, we do something called input.
- 02:14 And input does work in Python 3, it's the thing you're supposed to use.
- 02:18 And we can confirm this by going what's your name?
- 02:21 12, hello 13 because we added 1 to it.
- 02:24 Now the problem with input is if we save this and run it again and
- 02:28 we say what's your name and we type in John,
- 02:31 we get an error because input is looking for an integer not a string.
- 02:34 So what do you do?
- 02:36 Well, we can do this stdin thing in order to get a string in the correct way for
- 02:41 Python 3.
- 02:42 But we need to import a systems module for that.
- 02:45 So let's import sys for systems.
- 02:48 Now to use this we just go sys.stdin.read line and then pass it, whatever.
- 02:57 Now there's no prompt here.
- 02:58 So we have to print out a prompt if we want one.
- 03:01 So what's your name?
- 03:03 Save this and run it.
- 03:06 What's your name?
- 03:08 John.
- 03:08 Hello, John.
- 03:09 Run it again, what's your name?
- 03:11 12, Hello 12.
- 03:13 But if we go namer +=1 and then try and run it, we get an error because
- 03:17 again this is converting anything you type into a string.
- 03:21 So again, if you want to use numbers, integers, you have to use the input.
- 03:24 So that's how you get user data, a little bit tricky.
- 03:27 You're not supposed to use raw input because it's deprecated for Python 3, but
- 03:31 it still works.
- 03:32 So I am going to keep using it.
- 03:34 But if you're a little squeamish about that, just go ahead and
- 03:37 use this stdin thing.
- 03:38 Standard input, I think, it stands for.
- 03:39 Just be sure to import your system's module to do that.
- 03:43 So pretty easy, pretty straightforward.
- 03:45 In the next video we're going to look at opening and closing files with Python.
Lesson notes are only available for subscribers.