160 likes | 200 Views
Selection. Learning objectives. Explain that selection is related to decisions in programming Use if …. Else if ….and Else programming constructs. Making decisions.
E N D
Learning objectives • Explain that selection is related to decisions in programming • Use if …. Else if ….and Else programming constructs
Making decisions Nearly every complex computer program has to make decisions. Every day we make choices too. Consider this, if it is cold you might want to wear a coat, but if is hot you will not, so in pseudocode this might be expressed as: If the weather is cold Wear a coat Otherwise Do not wear a coat In programming selection allows us to make these decisions
Selection Selection represents a decision in the code according to some condition. So if the condition is met then the block of code is either executed or it is not. Often alternative blocks of code are executed according to some condition. An integral part of selection are relational and Boolean operators that we have already looked at. If some condition is true do something else if some other condition it true do something else else do this thing instead
Making decisions in Python (1) The simplest form of selection in Python contain an If clause so this would be: if age < 16: print (“You are a child”) The relational operator age < 16 is used to determine whether the print statement is executed. It is good practice to be explicit when the condition as not bee met (ie the age variable is greater that 16), so we would have something like: if age < 16: print (“You are a child”) else: print (“You are an adult”) That also incluse the else clause. In Python you need to complete the if and else clauses with a colon (:). The statements within the if else construct need to be indented.
Making decisions in Python (2) A slightly more complex construct now uses if, else if (elif) and else clauses. if age < 13: print (“You are a child”) elif age>=13 and age <=19: print (“You are a teenager”) else: print (“You are an adult”) Notice now for line 3 we have both Boolean and relational operator working together.
Write up the following program. What does it do? hour=900 if (hour>= 500 and hour< 1200): print (“good morning”) elif (hour>= 1200 and hour <=1700): print (“good afternoon”) else: print (“good evening”)
Create a Quiz Extend the quiz so that it totals up the score out of 10. You will have to use increment a variable that keeps score e.g. score=score+1 Create a quiz that asks the use a series of 10 questions. answer1=input(“Which city is the capital of Australia?\n") if (answer1 == "Canberra“): print ("Well done!") else: print ("You need to revise your geography") print ("The answer is Canberra")
Student grades Write a program that asks for the name of a student and the mark of the students in the test and then outputs the grade of the students according to the following criteria. 81-100 A* 71 – 80 A 61-70 B 50-60 C 40-50 D 0-40 U
# 22/01/15 # teacher grades mark=int(input("What was the pupils mark?")) if(mark>=90): print("A*") elif(mark>=80and mark<90): print("A") elif(mark>=70and mark<80): print("B") elif(mark>=60and mark<70): print("C") elif(mark>=50and mark<60): print("D") elif(mark>=40and mark<50): print("E") elif(mark>=30and mark<40): print("F") else: print("U")
What is output in each case? Question 1 A=1 B=2 if A < B: print (“Hello”) elifA > B: print (“Wagwan”) else: print (“Hi”) Question 2 i=9 j=3 if i < j: print (i is smaller than j) elifi > j: print (i is larger than j) else: print(j is the same as j) Question 3 x=4 y=4 if x < y: print (x is smaller than y) elifx > y: print (x is larger than y) else: print(x is the same as y)
What is output in each case? Question 5 i=9 j=3 k=1 if ((i<j) and (k>i)): print (i is smaller than j and k) else if (i>j and (i<k)): : print (i is larger than k) else: print(k is the same as i) Question 4 A=1 B=2 C=3 if ((A<B) and (C<B)): print (“Hello”) else if ((A>B) and (C>B)): print (“Wagwan”) else if ((A>B) and (C<B)): print (“Hi”) else: print (“Hello”) Hello I is larger than j X is the same as y Hello i is larger than k
What is output in each case? if num1<num2: if (num3>num4): print(“a”) else: print(“d”) elif num1>num2: if (num3==num4): print(“b”) else: print(“e”) else: if (num3<num4): print(“c”) else: print(“f”) num1=2, num2=2, num3=2, num4=2 num1=2, num2=3, num3=4, num4=5 num1=5, num2=4, num3=3, num4=2 num1=5, num2=2, num3=1, num4=1 num1=2, num2=3, num3=2, num4=3 f d e b d
Good day program Write a program that asks you for your name than then replies with: “”Good morning , name” (before 12pm) “Good afternoon, name” (between 12 pm and 5 pm) “Good evening, name” (after 5pm) import time hour = time.gmtime()[3] # to convert to bst hour=hour+1 print(hour)
Plenary: Solid, liquid, gas, what is output for each situation? absolute_zero=-273 if temp >= absolute_zero and temp <= melting_point: print(“Solid”) elif temp >melting_pointand temp < boiling_point: print(“Liquid”) elif temp >= boiling_point: print(“Gas”) else: print(“Not Found”) melting_point=0, boiling_point=100, temp=67 melting_point=0, boiling_point=100, temp=-240 melting_point=13, boiling_point=47, temp=67 melting_point=-12, boiling_point=76, temp=-17 Liquid Not found Gas Solid
Coding test: input, output, selection, operators and assignment 1) The program needs to have comments. 2) The program needs to ask for your: • Name • Age (hint: this is an integer) • Address • Hint: catName = input (“what is your cat’s name?”) 3) The program needs to output your name, age and address • Hint: print(“Your cat’s name is ” , catName) 4) Write an if statement that states whether you are a child (less that 16) or adult. Hint: elephants = 10 If elephants == 10: print(“you have all your elephants”) else: print(“you have lost some elephants”)