Learn To Program: Variables and User Input
Learn To Program Tutorials
For these tutorials, you will need Python. This is Tutorial 2 of this series.
Click here for Tutorial 1 where you will learn about Python, how to install Python, how to use IDLE and create your first program.
The Hello World program is good but is not very interesting. The only thing that happens is that a piece of text is printed on the screen of your computer. We need to make our programs much more interesting! But how?
Well, first of all, to make a program more interesting, we need the computer to remember something like our name or our favourite colour. We can then ask Python to say "Hello! Nice to meet you...", followed by our name. That would personalize things a lot more.
To get Python to remember something for us, we store whatever it is we want to store in a variable. A variable is a word that stores the thing we want to store. So, we might store our name within a variable, which will be used again later within our program. Variables will become very handy in our programming endeavours!
Legal Python Variable Names:
- The beginning of a variable name can only begin with an underscore (-) or a letter.
- Variable names can only contain numbers, letters and the underscore.
Let's Create A Variable
You can call a variable whatever you want, preferably a word which you will know what is stored in it when re-reading your program again. So if we want to store our name in a variable, I would call the variable my_name or myName. Think of the name you will use but keep the following rules to the right in mind.
Assigning Your Variable To A Name:
To store your name within your chosen variable name, you:
- Write the name of your variable
- Add an equal sign on the same line. This will tell Python that you want to store your chosen text or number within that variable or in other words, that variable is equal to the text or number you have stored in it.
- After the equal sign, add the text or number that you want to store to the line.
- If you want to display your code write 'print' followed by your variable which will display whatever is stored in it.
The result of following the steps above is shown in the code below. Simply type the following code into Python's New Window and press save and run it. Make sure to write your name in the "yourname" space.
Below is an example of storing the word 'Susan' in the variable 'my_name' and then printing it. Let's see what happens.
my_name = "Susan" print (my_name)
Using what we have learned from this small program, let's make a more interesting program out of it. I am going to get the computer to say hello and then my name, to add a more personalized touch.
my_name = "yourname" print('Hi there!') print ('It is nice to meet you, ' + my_name) print(' Bye for now, ' + my_name)
The Code Explained
The first line is known as an assignment statement. You are assigning or storing your name within that variable. Remember to put quotes around your name as it is a string!
The computer then prints a greeting and then it says that it is nice to meet you and adds your name to the sentence which is stored in the variable my_name. Notice that the string in line 3 is ended and then the variable is added on. You do not add the variable as part of the string.
Let's say that you wanted your computer to ask you for your name and then print 'Nice to meet you' followed by whatever you have typed ie: your name. To create this program, Python will be asking for user input. That input that you give to Python will be stored within a variable. Then, Python will be able to print that variable again. To ask a user for input and then store it in a variable, the following line of code is used.
user_name = input()
Time Module and Printing Blank Spaces
Before we make a full program based on this, firstly we should learn about creating blank spaces and placing a time delay. Instead of all the text coming out in one big block, we can have each line printed separately and with a time delay.
After each line, we will have to put a print() . The brackets are empty as we only want to print a space with no text. To have a time delay, we will need to import a module. This module is called the "time module" and to import it, at the start of our program we will put "import time". Then, every time we want a time delay between the printing of each line, we add time.sleep(1) after each line. The number in the brackets is the amount of seconds that you want to elapse.
Let's make a full program out of this incorporating what we have learned.
import time print("What's your name?") my_name = input() print('Hi there!') print() time.sleep(1) print('It is nice to meet you, ' + my_name) print() time.sleep(1) print('Bye for now, ' + my_name)
Technical Stuff: What is a Module?
A module is a file in Python's library which contains Python definitions and statements. If you created a formula and you had to use it more than a couple of times across multiple programs, you will have to create a module. You can then access that module simply by importing it.
Hubbers and Guest Users To Complete The Challenge
Here is a list of those who have completed the challenge with perfect code. Well done to all!
- drpennypincher - He got his code in first and it was great. Well done!
After this lesson and Lesson 1, you have learned a lot so well done! Try composing programs which ask for user input and then print these programs. But if you want an exercise, try the challenge below. If you know how to do it, just tell me in the comments and paste your code as well. Let's see how many of us can do it! Here is the challenge.
The Challenge: Your name is Mr/Ms.Jones and you have approached an ATM machine. Write a program based on how the ATM program would be. Here is an outline of what is expected.
- Welcome to Universal Bank!
- What is your name?
- Nice to meet you ....
- How much would you like to withdraw (name)?
- Taking (amount).... from your account.
- Done! Please take your ...(amount)
This program isn't the most advanced it could be but over the weeks, you will learn how to make it better and more impressive. Make sure to incorporate the time module and blank spaces. Good luck!
In The Next Tutorial...
Our next Tutorial which is the 3rd in this series, will focus on if statements, how Python makes decisions and while loops as well as making a simple game.
© 2013 Susan W