Presentation is loading. Please wait.

Presentation is loading. Please wait.

Summary Conditional: if .. else New Nested conditional elif

Similar presentations


Presentation on theme: "Summary Conditional: if .. else New Nested conditional elif"— Presentation transcript:

1 Summary Conditional: if .. else New Nested conditional elif
Repetition -- while

2 Pictorial view of how if works
if (expression): true false next indented block else: Statements

3 Grades Suppose we have a letter-grade system: 0<= grade <=50 – D
Perfectly fine with 0<= grade <=50 – D 50 <grade <=70 – C 70 <grade <=90 – B 90 <grade <=100 –A if 0<= grade <=50: letterGrade =‘D’ if 50 <grade <=70: letterGrade =‘C’ if 70 <grade <=90 : letterGrade =‘B’ if 90 <grade <=100 : letterGrade =‘A’ print(‘The letter grade is ‘ +letterGrade)

4 Multi-way branches Conditions are mutually exclusive
if 0<= grade <=50: letterGrade =‘D’ else: print(‘The letter grade is ‘ +letterGrade) if grade <=70: letterGrade =‘C’ else: if grade <=90: letterGrade =‘B’ else: letterGrade =‘A’

5 Multi-way branches with elif
if 0<= grade <=50: letterGrade =‘D’ elif grade <=70: letterGrade =‘C’ elif grade <=90: letterGrade =‘B’ else: letterGrade =‘A’ print(‘The letter grade is ‘ +letterGrade) if 0<= grade <=50: letterGrade =‘D’ else: if grade <=70: letterGrade =‘C’ if grade <=90: letterGrade =‘B’ letterGrade =‘A’ print(‘The letter grade is ‘ +letterGrade)

6 Another Multi-way Branches
if 0<= grade <=70: if grade <=50: letterGrade =‘D’ else: letterGrade =‘C’ if grade <=90: letterGrade =‘B’ letterGrade =‘A’ print(‘The letter grade is ‘ +letterGrade) if 0<= grade <=50: letterGrade =‘D’ else: if grade <=70: letterGrade =‘C’ if grade <=90: letterGrade =‘B’ letterGrade =‘A’ print(‘The letter grade is ‘ +letterGrade)

7 While if vs. while count=0 if count< 5: print(“Hello!’)
count= count+1 count=0 while count< 5: print(“Hello!’) count= count+1

8 How if & while works While (expression): if (expression): True True
False False Statements Statements

9 Guess a number in 5 times Within 5 trials, guess the name ‘kim’ and quit if correct count=0 while count< 5: name=input(‘Enter a name.’) if name==‘kim’: count=5 count= count+1 count=0 while count< 5: print(“Hello!’) count= count+1

10 Guess a number in 5 times Within 5 trials, guess the name ‘kim’
count=0 while count< 5: name=input(‘Enter a name.’) if name==‘kim’: break count= count+1 count=0 while count< 5: name=input(‘Enter a name.’) if name==‘kim’: count=5 count= count+1

11 Guess name and password
Within 5 trials, guess the name ‘kim’ and password def logIn(): count=0 while count < 5: count= count+1 name=input(‘Enter a name.’) if name !=‘kim’: continue passwd = input(‘What is password?’) if passwd == ‘xxx’: break if count < 5: print(‘Access granted’) else: print(‘Five attempts failed.’)

12 HW 2-- Guessing game (Due 9/21)
A random number between 1 and 10 is obtained from import random randNum = random.randint(0,10) Write a function guess(), which generates a random integer between 1 and 9 (inclusive) and let a user enter a number up through five times to match the random number your working Python file (guess.py) to def guess(): import random randNum = random.randint(0,10) .


Download ppt "Summary Conditional: if .. else New Nested conditional elif"

Similar presentations


Ads by Google