yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

User input | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

What are some of the ways you interact with digital technology every day? You might press a button, enter something into a text box, or swipe up or down. You might even move a joystick on a controller, tap a credit card, or turn a knob on a car. These are all ways that a program can ask for input from a user. The program waits for the user's response and then reacts to it.

In Python, we can ask the user for input with the input function. When I run this program, I see a blinking cursor appear in the console. If I type something in here and hit enter, the program terminates. But what happened to that value that I entered? It's there. The computer received it, but just like any value, we can't see it unless we tell the computer to print it.

When we use the input function, we want to think about our user interface. The person using our program may be someone else who doesn't know anything about our code, just how we don't know how the programming behind most of the technology we use every day works. We just know how to use it. So we need to make sure we're giving the user enough information so they know what to enter when they see the prompt.

Luckily for us, the input function takes a single input value. Whatever string we put inside these parentheses will show up in the console on the same line right before that blinking cursor. We want to make this prompt message descriptive so the user knows what we're asking for. Maybe they need to choose between a few values or enter a date.

Here, let's imagine we're building a weather app, and we want the user to enter a city so we can load the weather for it. I'll add a descriptive prompt here to make sure the user knows we're looking for a city name. By convention, we usually add a single space character at the end of any prompt message, so we get a space in the console between the message and the cursor.

At the top of the app, I want to display "Weather for city name." So I'm just going to concatenate that to the front of the input call because we know the input function returns whatever string the user entered at the prompt. What if I also want a section on the page to display "City name 10-day forecast?" If I add another input function call, now two prompts show up in the console, so the user has to enter the city name twice, and there's no guarantee that they enter the same city name both times.

Instead, sounds like we want to store that first city name that the user entered so we can use it later. This calls for a variable. In most cases, this is the first thing you want to do with the output of an input function because we want to save that information that the user gave us. Then I can just access that variable anywhere I want to use the user's input.

You may be thinking, "Hold up, you were talking about tapping a credit card or swiping on my phone, not typing something into this janky prompt in the console." It's not because we're building fake programs; it's because we're working on the back end of an application. Most digital technology has a front end and a back end. The back end is what the application does; it's the main logic in the data processing.

The front end is what the application looks and feels like; it's the visual or tactile elements that the user interacts with. The front end and the back end then communicate. The front end tells the back end what actions the user took; the back end processes that information and then tells the front end what data to display. Then the front end takes that data and decides how to display it to the user.

Both parts are equally important. If I have a game where the controls are super confusing and show up in unexpected places on the screen, then no one's going to play my game, even if it's the coolest game ever. On the other hand, if I have a beautifully designed website with an easy-to-navigate layout, but the search results are garbage and everything's slow to load, no one's going to use my application either.

Some programmers work on both backend and front-end, but many programming teams are divided into backend developers and front-end developers because they're both such critical but different skill sets. Python is primarily a back-end programming language, so when you use the input function, imagine there's a front end that goes with it.

If you want to handle touch gestures on a phone, maybe your input prompt asks the user if they want to swipe right, left, up, or down. Or if it's a text box, then maybe the prompt is just the label for it, like "Search" or "Subject." You can imagine your print output the same way: the back end's just returning the plain data, but the front end would lay it out nicely on the screen.

So if you ever start feeling like, "Hey, this console input-output thing feels kind of blah," remember that most programs don't look fancy either. It's thanks to front end developers that applications look cool and easy to use, and it's thanks to back end developers that they do complex and interesting things.

Let's focus on mastering one thing at a time, though. For us, that means making our programs do cool things, and we'll worry about how they look later.

More Articles

View All
"The SECRET To Business Success In 2021 IS THIS!.." | Kevin O'Leary & Gary Vee
So in this crazy outcome that I could have never foreseen, 80% of the portfolio, even though revenues are down, are actually ahead of free cash flow forecasts. You cannot, people, you can’t lie to them. You have to be totally transparent, whether that’s g…
Why is Deadly Weather Mesmerizing? | StarTalk
Well, in the same way that CNN does very well in their ratings when there’s war, the Weather Channel does really well when there’s extreme weather. Right. So people love watching extreme weather—the tornadoes—it’s mesmerizing. Hurricanes. Absolutely. And …
What Happens to Lasers Underwater? (Total internal reflection) - Smarter Every Day 219
Hey, it’s me, Destin. Welcome back to Smarter Every Day. If you can’t tell, I am on a kayak here. And we have a lot of the kayak. I guess it was a week ago I uploaded a video to the second channel. And we were trying to fish, right Trent? [Trent] Yeah. Oh…
Intro to forces (part 1) | Physics | Khan Academy
A force is just a push or a pull, that’s it. But in this video, we’re going to explore the different kinds of pushes and pulls that we will encounter in our daily lives. So let’s start with an example. Imagine you are pulling a chair in your living room u…
Summiting the World’s Most Dangerous Mountain | Podcast | Overheard at National Geographic
We’re high on a snowy mountain in Pakistan where a group of Nepalese climbers are struggling through harsh winds. It’s two o’clock in the evening. Think this is one of the hottest climbs we have ever met. [Music] That’s Ming Maggioja Sherpa. He goes by …
Perfect Aspect | The parts of speech | Grammar | Khan Academy
Hello Garans. Today I want to talk to you about the idea of the perfect aspect of verbs. What that means is that it’s not, you know, beyond reproach or that it’s like beautiful and shiny. No, no, no. What it means is really that whatever we’re talking abo…