[TUT] LEARN THE BASICS OF PYTHON. MAKE A PASSWORD PROGRAM [NOOB GUIDE]

Hello everybody. Today I am going to be teaching you how to make quite a basic Python program.
We will be making a password program, which will only allow you access if you enter the right password.
HOW TO MAKE A SINGLE-USER PROGRAM
Ok so I for this to work we will have to use Python 2.*, not 3.*. Second, just for the record, im using the IDLE compiler, the one that comes with the official python installation. So lets start off the code. In this first example we are going to make the password just numbers. The first line in our code will be the menu. So lets print a line, which will appear at the top of the command shell:
Code:
Code:
print "\n\n---WELCOME TO BankLock\nVersion 1.0\n"
Now we're going to breakdown that code. In python when you want to print a line you write in
Code:
Code:
print "your text"
just replace 'your text' with whatever you want to print.
NOTE: Keep the apostrophes in there, they define where the text to be printed ends.
Now you may wonder what all the \n things are. If you save what we've already written and save it as a .py file, you'll see. Everytime you put a \n in, a new line is inputted into the console.
NOTE: The \n must be within the apostrophes of the print line
So the output you should see (of our code) is something like this:
Code:
.
.
---WELCOME TO BankLock
Version 1.0
.
Except without the fullstops. Now to make the user put in his password:
Code:
Code:
usrpass = input("Please enter the password:\n")
So what this lind of code does is it takes the number that the user types in, then assigns that number to a variable, in this case, usrpass. usrpass can then be called on at any time later in the script and it will always represent the number that was assigned to it, unless it was changed. Now comes the part of comparing the number password that the user put in, and the one that we have set as the real password. But first, we need to make the variable that will be compared to (the true password). To do this we write:
Code:
Code:
realpass = 99
Of course, realpass can be whatever you want the password to be. Now we will compare them.
Code:
if usrpass == realpass:
    print "Access accepted. Correct password entered.\n"
    raw_input("Press <enter> to exit.")
This bit of code compares usrpass (what the user entered) and realpass (what we made the password) and then says 'if they match each other, then execute this piece of code:'. Notice that the tab shows which bits are to be executed if they match. We also see the raw_input() bit. That means that when we press any key the program will shutdown. We use it to stop the program automatically closing. But this code is missing a bit. That bit is what to do if the code is wrong. To complete this bit, we will put in:
Code:
Code:
elif usrpass != realpass:
    print "Access denied. Incorrect password entered.\n"
    raw_input("Press <enter> to exit.")
Now i'll breakdown this last section of code. You may notice that instead of ==, we use != this time. That means that 'if the two dont match' instead of 'if they do match'. The other options you can use are
Code:
x >= y    (if x is larger than y)
x <= y    (if y is larger than x)
This means that if the password entered does not match the real one, the following commands will be executed. We also recognise the print and raw_input() commands.
So now if the final script should be put together like this:
Code:
Code:
print "\n\n---WELCOME TO BankLock\nVersion 1.0\n"
usrpass = input("Please enter the password:\n")
realpass = 99
if usrpass == realpass:
    print "Access accepted. Correct password entered.\n"
    raw_input("Press <enter> to exit.")
elif usrpass != realpass:
    print "Access denied. Incorrect password entered.\n"
    raw_input("Press <enter> to exit.")