Python 2: Conditions
By the end of this lesson you should be able to:
1. Describe how tab is used to define blocks in Python
2. Set up an if-statement
3. Use a variety of conditional operators (<,>,==..)
For a programming language to be useful it needs to be able to compare values and act on the result. The most common structure for this is the “if-Statement”.
If something is true do this
Note 1: To tell Python what you want to do in the ‘true’ case you need to hit tab once on each line. This is one of the features that sets Python apart from other languages, it forces you to have perfect form.
You can also link conditions together using the key words ‘and’ and ‘or’.
Save each program in you unit 1 folder as a text document called A2Px where x is the program number. Then copy and paste it into the python interpreter. Use the # symbol in include your name and the program code at the top of your script.
Ex: #Tony Demello A1P1
Program 1 – Positive, Negative or Zero
Write a program that prompts the user to enter an integer. Tell the user whether it is positive, negative or zero. Hint: Positive numbers are >0
Program 2 – Password
Prompt the user to enter a user name and password. Check them against a user name and password in your program to tell them whether access is granted or denied.
Program 3- Average Score Mark II
Prompt the user to enter 5 scores between 0 and 100. Calculate the average and display the result. With an appropriate comment in these ranges:
Show Mr.Kurz all 3 programs when you are finished.