Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ordering of Conditions There is an issue with multiway decision structures that does not exist with one or two- way if statements. The order in

image text in transcribedimage text in transcribed

Ordering of Conditions There is an issue with multiway decision structures that does not exist with one or two- way if statements. The order in which the conditions appear in a multiway if statement is important. To see this, try to figure out what is wrong with the order of the conditions in the next implementation of the function temperature(). def temperature(t): if t > 32: print('It is cool.) elif t > 86: print('It is hot!') else: print('It is freezing!') The problem with this implementation is that 'It is cool' will be printed for all values of t greater than 32. So, if t is 104, what is printed is 'It is cool.'. In fact. 'It is hot!' will never get printed, no matter how high the value of t is. The issue is that conditions t > 32 and t > 86 are not mutually exclusive, as conditions corresponding to code blocks in a two-way decision structure are. One way to fix the wrong implementation is to make the conditions mutually exclusive explicitly: def temperature(t): if 32 86: print('It is hot!') else: t 86, 32 86. Any subsequent condition in a three-way if statement will be tested only if the first condition fails (i.e., the value of t is no more than 86). Therefore, any subsequent condition includes, implicitly, condition t 32 is really 32 32: print('It is cool.) elif t > 86: print('It is hot!') else: print('It is freezing!') The problem with this implementation is that 'It is cool' will be printed for all values of t greater than 32. So, if t is 104, what is printed is 'It is cool.'. In fact. 'It is hot!' will never get printed, no matter how high the value of t is. The issue is that conditions t > 32 and t > 86 are not mutually exclusive, as conditions corresponding to code blocks in a two-way decision structure are. One way to fix the wrong implementation is to make the conditions mutually exclusive explicitly: def temperature(t): if 32 86: print('It is hot!') else: t 86, 32 86. Any subsequent condition in a three-way if statement will be tested only if the first condition fails (i.e., the value of t is no more than 86). Therefore, any subsequent condition includes, implicitly, condition t 32 is really 32

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

Solve the following 1,4 3 2TT 5x- 1+ (15 x) dx 5X

Answered: 1 week ago

Question

Stages of a Relationship?

Answered: 1 week ago