03 - Getting User Input

One of the most important parts of programming is user interaction. There are many ways in which programs and applications interact with their users. For example, a game will allow a user to press certain buttons, or you can create an account on a website…

And these are all forms of β€œuser-input”.

The Input Function

The input function allows for the user to type in text, and it captures that text and returns it to you as a string.

Two ways to use the input function - with an argument or without an argument

Calling the input function with an argument

If you call the input function with an argument, the argument must be a string.

The argument serves as a prompt for the user.

greeting.png
rungreeting.png

result from running code in Terminal

Calling the input function without an argument

othergreeting.png

I included a print statement as a prompt before calling the input function.

runothergreeting.png

result from running code in terminal.

If you choose to call the input function without an argument, it’s helpful to include a print statement beforehand, so that the user knows what they’re expected to type.

Previous
Previous

02 - Data Types, Variables, If-Else, Comments

Next
Next

04 - Objects, Type Casting