In my first python tutorial I taught you how to declare variables, in the second I taught you selection, and in the third I taught you while loops. Let’s put these together now to create a simple login program.

This script will give the user three chances to enter the password. If the user gets it right it will welcome them, and will tell the user if they get it wrong.

image

The first thing we do is declare a variable ‘count’ with a value of 0. This variable will keep a count of how many guesses the user has had.

After that we loop while count is less than 3. Less than (<) is non-inclusive, which means it doesn’t include the number that our variable is being compared to. The while loop will execute if count is 0, 1, or 2.

On the next line we ask for input from the user, and save that in a variable userPass. We then compare userPass with our password. If userPass is the same as (==) “UndedIs1337″ then “Welcome Unded” is shown, and the break statement runs, otherwise it will tell the user they got it wrong and add 1 to count.

I didn’t discuss breaks when I taught about while loops. A break simply stops the loop. The condition of the while loop doesn’t have to be met - the break just breaks the loop instantly.

Changes you could make:

  • Change the number count starts at.
  • Increase the number count increments to in the while statement.
  • Change the password.
  • Change the output.

You now have an idea about the very basics of python. There is plenty more that I will go into, as well as revisiting these basics to ensure that you have a solid understanding of it.

As always, if you have any questions, corrections, or comments feel free to leave a note, send me a message, or get in touch with me over twitter @UndedInside