Interacting With User Input
In this lesson, you’ll learn how to get some input from a user with the built-in
input() function. You’ll write a program that asks a user to input some text and then display that text back to them, and you’ll also practice applying some of those methods that you learned in the last lesson. All right, the rest of this lesson is all going to be inside of IDLE.
A newline appears, and it prompts you on the next line to type something—in this case, the user, or whoever’s running this. So I’ll see
Hello There! And when I press return, it returns a string literal of
Hello There! To make
input() be a little more user-friendly, you might have noticed that it asked that I could provide a prompt to display to the user.
01:57 And again, it returns the string literal of what was typed in by the user. Let’s change this up and create a program, one that we can define the prompt, and then also maybe print out a a message using some of the skills you’ve learned up to now.
02:17 I’m starting with a brand-new interactive window. I need to have you open up IDLE’s editor window. You can go to File and New, or you can use a key command, which is what I’m going to use here, which is just Command + N, or on Windows, it would be Control + N. That creates an empty editor window.
putting the two windows side by side here. So inside the edit window, you’re going to start with a prompt you’ll insert into the
input() function, and the prompt will be pretty simple:
"Hey, what's up? " And again, remember the little space there at the end of it so that you can leave room for prompting. Then you’ll accept that into
user_input is going to be a variable that you create that is assigned to what is returned from the built-in
input() function. Now, the one thing that’s different here is again, you can put a prompt in. We’ve literally named it
input() will prompt with that, and that’ll be assigned, whatever’s typed into
user_input, and then you can print it out. So again, you may want to leave a space because you’ll use your skill of concatenation with the
+ (plus sign) there, and then it will print out the
user_input. Great. So again, you’re assigning a string to the literal
what's up? ", using that inside of the
input() prompt to assign to
user_input, and then printing that out.
03:55 Okay. Do you remember how to run programs here? You can use the key command of F5, or you can go to the menu up top that says Run. I’m going to just press F5 and then over here in the interactive window, you’ll see it run. Okay, so there it is.
Just working, and pressing Return, it replies it. Great. Pretty simple program, but now you’ve learned how to use user input. What if you use that knowledge to do something a little more interesting, change the functionality a little bit, use some string methods?
So it’ll still get accepted into
user_input, it’ll prompt with that. But what we can do is change this a little bit to make it be shouted. You could add a string method. So
user_input, change it to use the
.upper() method, which will make everything uppercase.
05:34 I think you’re ready to explore user input a little bit deeper on your own. Here’s some review exercises for you. Try writing a program that takes input from the user and then displays that input back, kind of like what you’ve done already, but change it up a bit.
05:51 Try writing a program that takes input from the user and then displays that input in lowercase. Try writing a program that takes input from the user and then displays the number of characters, counting the characters of that input that was typed in.
Become a Member to join the conversation.