How to start programming with Python, Part 3. Variables, and Input!

in #programming6 years ago (edited)

Previously I showed you a basic python command.

print("Hello World!")

This command outputted a line of text to the console stating "Hello World!"

In this part of the python series I will teach you how to receive user input and assign it a variable.

Start up your "IDE" or text editor.

If you don't have one I would recommend Notepad++, it works as a text editor and IDE.

Then open up the file from the last tutorial "MyFirstProgram.py" (or whatever you called it).

If you don't have this file, make it now.

Firstly, what do we want to accomplish? We want to recieve user input and assign it to a variable to use later.

The correct command to achieve this would be:

input()

Now if you added this to your program (like this):

print("Hello World!")
input()

It won't do much. Your program will have a blinking cursor underneath the "Hello World!" text.

If you press enter it goes away. If you type anything and press enter it goes away.

We need to save the input because at the moment anything we type gets deleted.

We do this by storing the user input in a variable. Variables are interchangeable values that can be used to store data. We can also output these variables as text with the "print()" command

Here is an example of a variable and usage:

x = "hi"
print(x)

If we ran this program, we would recieve an output in the console of "hi".

Now let's see if we can store input in this variable.

x = input()
print(x)

Add this to our program and see what happens.

It should give you a prompt, and when you enter text in the prompt the text is repeated back to you.

Personalization

Let's personalize the code. Make it ask us for input more formally and repeat it back nicely.

Our code is as follows:

print("Hello World!")
x = input()
print(x)

If we adjust it to this:

print("Hello World!")
x = input("Input needed: ")
print("Here is your input: " + x)

Then it should look like this:

We can do lots of things with variables, but that's for next time.

Next time we will string together variables, create functions, and do basic calculations.

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

You have a minor misspelling in the following sentence:

### In this part of the python series I will teach you how to recieve user input and assign it a variable.
It should be receive instead of recieve.

Coin Marketplace

STEEM 0.24
TRX 0.11
JST 0.032
BTC 61166.56
ETH 2987.63
USDT 1.00
SBD 3.71