Continue with conditional

22

description

Continue with conditional. If statements-example 1. - PowerPoint PPT Presentation

Transcript of Continue with conditional

Page 1: Continue with conditional
Page 2: Continue with conditional

Problem : At Kiddy School , the teachers used the A, B,C for grading the students’ works. If the student get an A, then the teacher will print “Excellent” and if the student get B, the teacher will print “Good”. For students that get C, the teacher will print “Work Harder”.

Given the problem above, design a solution using flowchart.

Page 3: Continue with conditional

Solution

Yn

Yn

Page 4: Continue with conditional

>>> grade=raw_input('enter student grade')enter student gradeA>>> if grade =='A':... print "Excellent"... elif grade =='B':... print "Good"... else:... print "Work Harder"...Excellent

Input is string therefore use

raw_input function

User input A

String –characters is compared lexicographically

order, hence must put capital letter

result

Page 5: Continue with conditional

>>> grade =raw_input('enter student grade ')enter student grade a>>> if grade=='A' or grade =='a':... print 'excellent'... elif grade =='B' or grade =='b':... print 'good'... else:... print 'work harder'...excellent

‘a’ and ‘A’ have two different values. To

handle input either in capital letter or small letter, the condition is

joining with ‘or’ operator

Page 6: Continue with conditional

Problem: Your instructor asked you to design a

program that can receive two input number (assumed that both number are not the same) and determine the larger of the two.

Design a flowchart the problem above.

Page 7: Continue with conditional
Page 8: Continue with conditional

>>> a=input('input the first integer')input the first integer2>>> b= input('input the second integer')input the second integer3>>> if a > b:... print a... else:... print b...3

Input by user

Input by user

Make sure the proper

indentation is used

Result after comparison

Page 9: Continue with conditional

Problem:Your instructor asked you to design a

program that can take 3 input and determine the largest of three input integers.

Draw flowchart and write Python code for the problem above.

Page 10: Continue with conditional
Page 11: Continue with conditional

>>> a=input(' enter the first number :') enter the first number :2>>> b=input(' enter the second number :') enter the second number :3>>> c=input ('enter the third number :')enter the third number :1>>> if a > b:... if a> c:... print a... else:... print c... elif b > c:... print b... else:... print c...3

Page 12: Continue with conditional

Using the text given, draw a flowchart for mailing a letter:

http://chatt.hdsb.ca/~druivenm/computer/resources/start_html/ch02.htm

Start

Stop

Address the envelope

Put letter in the

envelope

Put the envelope in

the mail box

Put the local stamp

on

Put the airmail

stamp on

Is it airmail?

Seal the envelope

Go to the mail box

Page 13: Continue with conditional

Given the following text, construct a flowchart for changing a flat tire on a car.

http://chatt.hdsb.ca/~druivenm/computer/resources/start_html/ch02.htm

Put the spare tire

start

Put Jack & flat in

the trunk

stop

Jack up rear of

car

Remove the flat

tire

Jack up the front

of car

Lower the car

Remove the spare from the

trunk

Remove the jack from the

trunk

Is flat a front tire?

Page 14: Continue with conditional

Draw a flowchart for a Patient Intake Process in the clinic. Use all the texts presented below.

Patient in the system?

Patient arrives at Front Desk

Ask patient to be seated in the waiting room

Patient fill up the new patient form

Nurse ask for patient’s name and

search the database

Patient go to exam room

Patient leave the clinic

HIVQUAL Group Learning Guide

Page 15: Continue with conditional

solution

Page 16: Continue with conditional

The table below shows the normal boiling points of several substances. Design a program that prompts a user for the observed boiling point of a substance in degree Celsius ( C0 ) and identifies the substances if the observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message Substance unknown

Page 17: Continue with conditional

Substance Normal boiling point (Celsius)

Water 100

Mercury 357

Copper 1187

Silver 2193

Gold 2660

In your design :1.Identify the input2.The desired output3.Write the algorithm and pseudo code

Page 18: Continue with conditional

Input –the observed boiling point of a substance in degree Celsius ( C0 )

Output- identifies the substances if the observed boiling point is within 5% of the expected boiling point

Algorithm

Page 19: Continue with conditional

Input the boiling point if the boiling point is between 95 to 100 then

the substance is water else if the boiling point is between 337 to

357 the substance is mercury else if the boiling point is between 1128 to

1187 the substance is copperelse if the boiling point is between 2083to

2193 the substance is silverelse if the boiling point is between 2527 to

2650 the substance is gold else if the boiling point is more than 2650

and lower than 95 the substance is unknown.

Page 20: Continue with conditional

Keith’s Sheet Music needs a program to implement its music teacher’s discount policy. The program is to prompt the user to enter the purchase total to indicate whether the purchaser is a teacher. Music teachers receive a 10% discount on their sheet music purchases unless the purchase total is RM100 or higher. In that case, the discount is 12%. The discount calculation occurs before addition of the 5% sales tax. The sample outputs are as below: two output , 1 for teacher another one is not teacher.

Page 21: Continue with conditional

Total purchases RM122.00Teacher’s discount RM

14.64Discounted total RM 107.36Sales tax RM 5.37Total RM 112.73

Total purchases RM 24.90Sales tax RM 1.25Total RM 26.15

What you need to do?1.Analyze the problem- Identify input-Identify output-Identify formula2. Write a pseudo code3.Draw a flowchart4. Write a Python code

Page 22: Continue with conditional

Discuss in class