~ Decisions, Control Flow and IF, ELSE statements in Python


6 - Decisions and Control Flow -IF/ELSE

Decisions, Control Flow and IF, ELSE in Python

So far we have been declaring variables such as x and y and working with user input. Now it's time to look at how to control a program based on certain decisions. For instance, what if you wanted one thing to happen if the user entered a wrong username and another thing to happen if they entered the right one? This would require a decision or in Python the use of an IF ELSE statement. For example, if username =="hello", access granted, else, access denied. Let's look at the use of IF and ELSE statements in Python.

Intro Video

A little coding demo

Download Python 3 here:



Task 1

1.First, run the code to see how it works! This function simply asks the user for a secret code, and IF that secret code is equal (==) to the requried code, which in this case is 7777, then access granted.

2. Change the secret code to 4444. Test the result to see if it still works. Analyse the nature and set up of the IF and ELSE statements.

Code

def secretcode1():
     #Change the secret code to 4444. Test the result to see if it still works.
     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777":
          print("Access Granted, Welcome AGENT")
     elif code=="7776": #note the use of ELIF which is basically like ELSE IF
       print("Close, but not quite")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")
           
secretcode1()

Task 2

1.Run the code below and note how it works..

2. Here we are using the OR command to provide us with more options. Add a few more secret codes (assuming there are other agents at work!) e.g. 3342 or 0002. Test to see if they work as well when you add them to the mix using the OR command.

3. Change the code slightly so that the total variable adds up the three numbers x + y + z instead of just x + y.

Code

def secretcode2():
     #Here we are using an "or" to give more options. Add a few more secret codes,
     #assuming there are a few agents. e.g. 3322, 0007 and 0002. Test to see they work!
     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777" or code=="3333":
          print("Access Granted, Welcome AGENT")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")

secretcode2()

Task 3

1. This program has two lists. One list has expensive cars in it, and the other has cheaper cars. The program asks the user for their budget. IF they have a high budget which is over a certain amount, then display the list of expensive cars, else display the cheaper list!

2. Change the budget to over 5000 in order to display the expensive cars. Add a few more cars to each list, don't forget to add each car to the list in speech marks and seperated by a comma.

Code

def cars():
     #Change the budget to over 5000 in order to display the expensive cars
     #Add a few more cars to each list
     expensive_cars=["Mercedes","Audi","BMW"]
     cheap_cars=["Skoda","Maruthi","Nano"]

     budget=int(input("How much can you spend on a car?"))
     if budget>=2000:
          print("Here are some cars you could consider",expensive_cars)
     else:
          print("You may want to check out these brands:",cheap_cars)
     cars()

cars()

Task 4

1. This program is a straight forward login script. It asks the user for their username and password and if BOTH (note the use of the AND command) are right, then access is granted, else it is denied. It then also asks for a secret code.

2. Adapt and add to the program after the declaration of the secret code. Add an if statement here based on the one above. If the secret code is ==7777 then access is granted, else access is denied.

Code

def login():
     username=input("Enter username:")
     password=input("Enter password:")
     if username=="hello" and password=="open123":
          print("Access Granted")
     else:
          print("Access Denied")
     secretcode=input("...and one last thing: What's the secret code?:")
     #add an if statement here based on the one above.
     #If the secret code is equal to "7777", then access granted, else denied.
     
login()

Task 5

1.This program asks the user a few questions to see if they would be any good, or eligible, for any given option course (e.g. computing or history). The questions are "Do you do your homework?" and the expected answer, in this case, is "yes"!

2.Add another variable for input called "attendance". Ask the user if their attendance is always good, or has always been 100%? The answer should be yes, - add this to the mix of statements to produce the final answer

3. Ask for the user to also enter their current ID number. The ID must start with the digits "07"(using the @ as an example, create an if statment to check for this too"

def option_select():
     #Add another variable for input called 'attendance'. Ask the user if their attendance is always good. The answer should be yes, - add this to the mix of statements to produce the final answer
     #Ask for the user to also enter their current ID number. The ID must start with the digits "07"(using the @ as an example, create an if statment to check for this too")
     print("Answer the question to see if you are eligible to join the course:")
     hw=input("Do you do your hw?:")
     effort=input("Do you always put in a 100% effort?:")
     if hw=="yes" and effort=="yes":
          print("We'd love to have you on the course")
     else:
          print("Not sure about you on this course. Sorry!")
     email=input("We would like your email address for our records, Please enter it:")
     if "@" in email:
          print("That is a valid email, Thank you")
     else:
          print("That doesn't appear to be a valid e-mail.")

option_select()

Final Challenge

Use your knowledge and skills from the completed tasks. Use the trinket below to complete your challenge.

Barclays Bank requires a simple menu system that allows users to 1. register. 2. login and 3.quit. Create this menu system, and create a variable called choice that takes integer user input. If the choice is ==1 then jump to the register function. If the choice==2 then jump to the login function. Finally, if the choice==3, then jump to the quit function. You will need to create each of these three functions and put a simple PRINT statement in each of them e.g. "This is the login function".

Extension: Include functionality for customers to use an e-mail address and password for registration. Inside the function called 'register' ask users for their email address and password. If the email address is valid (contains) an @, and if the password contains contains the special character "#", then accept the details, else - reject registration. Use the message: Sorry, your email must have an "@" and password must include a #.

Answers

Coming soon!