9/4/2013 Announcements Quiz this Thursday. 15-112 Foundations of Programming Lecture 4 September 3rd , 2012 What we know so far How to get input from the user How to print messages Three type of values Basic Arithmetic Expressions Variables Functions Expressions Variables Input/Output Conditional Statements Homework 1 is due Today at 11:59pm Homework 2 will be posted tonight – Due September 10th. What do we need to learn? Sequential Execution Conditions Conditional Execution Repeating Execution 1 9/4/2013 Sequential Execution Conditional Execution All execution of instructions so far has been sequential Step 1 Example: print "Hello to my sequential program“ Step 2 name = raw_input("Enter your name> ") Sometimes we want to selectively execute statement If your name is same as mine, I should say something. name = raw_input("Enter your name> ") print "Oh Wow! Your name is same as mine" Step 3 age = int(raw_input("Enter your age> ")) Step 4 Is there anything wrong with this? print name,", you are ", age , "years old" Conditional Execution We can fix this by using conditional execution name = raw_input("Enter your name> ") Colon if name == "saquib" : print "Oh Wow! Your name is same as mine" Indented Conditional Execution Conditional execution finishes when you stop indenting name = raw_input("Enter your name> ") if (name == "saquib"): print "Oh Wow! Your name is same as mine" print "I really like our name" print "Welcome to my program", name >>> Enter your name> saquib Oh Wow! Your name is same as mine I really like our name Welcome to my program saquib >>> >>> Enter your name> Bob Welcome to my program Bob >>> 2 9/4/2013 Conditional Execution number = int( raw_input("Enter a number ")) if number > 0: print "The number is positive" print "Thank you for your number" >>> Enter a number 34 The number is positive Thank you for your number >>> More on forming conditions Conditional Operators and or not >>> Enter a number -5 Thank you for your number >>> Combining conditions num1 = int(raw_input()) num2 = int(raw_input()) num3 = int(raw_input()) if num1 > num2 and num1 > num3: print num1 if num2 > num1 and num2 > num3: print num2 if num3 > num1 and num3 > num2: print num3 Are you sick You are sick if your head hurts and you have a fever. If only your head hurts, you are just sick of this class which is not a real sickness Write a conditional statement that checks if you are really sick given two variables Headhurts Havefever 3 9/4/2013 Lets play doctor Being a doctor is easy If your head hurts, you have a fever, but not runny nose – you have the flu If you have a fever and a runny nose, but no headache – you have bronchitis If you don’t have fever, or runny nose, but only have a headache – you have brain tumor Can you write conditional statements to replace doctors? If else Sometimes we need to execute some alternate statement import math num = int(raw_input("Enter a number ")) if num > 0: print "Factorial is",math.factorial(num) else: print "You have entered an invalid number" >>> Enter a number 5 Factorial is 120 If-elif-else Sometimes we need to make mutually exclusive choices score = int(raw_input("Enter your score ")) if score >= 90: print "You have an A" if score >= 80: print "You have an B" if score >= 70: print "You have an C" if score >= 60: print "You have an D" if score < 60: print "You have an R" print "Now you know your grade" >>> Enter a number 0 You have entered an invalid number If-elif-else Fixed grades score = int(raw_input("Enter your score ")) if score >= 90: print "You have an A" elif score >= 80: print "You have an B" elif score >= 70: print "You have an C" elif score >= 60: print "You have an D" else: print "You have an R" print "Now you know your grade" 4 9/4/2013 Nested if condition Where are you from? Repetition Austin, Detroit, Tokyo, Ann Arbor Are your from North America? Austin, Detroit, Ann Arbor Yes No Tokyo Are your from Michigan? Detroit, Ann Arbor Yes No Are your from a university town? Yes Ann Arbor I think of a number between 1 – 100, you try to guess it How would you do this in Python? Austin No Detroit While loop While (some condition is true) : first statement second statement …. Outside of Loop Example Find the sum of all digits of a number entered by the user Inside the loop (Loop Body) 234 = 2 + 3 + 4 = 9 484 = 4 + 8 + 4 = 16 193 = 1 + 9 + 3 = 13 5 9/4/2013 For Loop Normally used when you want to execute some code a fixed or known number of times Example: Print all numbers from 1 to 10 for x in range(1,10): print x Example Print the sum of all numbers from 1 – 10 sum = 0 for i in range(1,11): sum = sum + i print sum Another Example of for loop Read a string from the user, print each character of the string on a separate line value = raw_input("Enter a string") for i in range(0,len(value)): print value[i:i+1] 6