Starting from:
$30

$24

Sheet 03: Conditional Execution Solution

State whether true or false:-

1) A simple decision can be implemented with an if statement.




2) In Python conditions ≠ is written as /=.




3) Strings are compared by lexicographic ordering.




4) A two-way decision is implemented using an if- el if statement.




5) The math.sqrt function cannot compute the square root of a negative number.




6) A single try statement can catch multiple kinds of errors.




7) Multi-way decisions must be handled by nesting multiple if- el se statements.




8) There is usually only one correct solution to a problem involving decision structures.




9) The condition x <= y <= z is allowed in Python.




10) Input validation means prompting a user when input is required.




Multiple Choice

1. A statement that controls the execution of other statements is called a a) boss structure




b) b) super structure




c) control structure




d) d) branch




2. The best structure for implementing a multi-way decision in Python is a) if




b) if- else




c) if- elif- else




d) try




3. An expression that evaluates to either true or false is called a) operational




b) Boolean




c) Simple




d) compound




4. The literals for type bool are




a) T, F




b) T rue, False




c) true, false




d) 1, 0




5. Placing a decision inside of another decision is an example of a) Cloning




b) Spooning




c) Nesting

procrastination



In Python, the body of a decision is indicated by



indentation



parentheses



curly braces



a colon



A structure in which one decision leads to another set of decisions, which leads to another set of decisions, etc., is called a decision



network



web



tree



trap



Taking the square root of a negative value with math.sqrt produces a(n)



ValueError



imaginary number



program crash



stomachache



A multiple choice question is most similar to



simple decision



two-way decision



multi-way decisions



an exception handler



Discussion:

For the following decision structure:




a, b, c = eval( input('E nter three numb ers : ' ) )







Show the output that would result from each of the following possible inputs:




a) 3, 4, 5




b) 3, 3, 3




c) 5, 4, 3




d) 3, 5, 2




e) 5, 4, 7




f) 3, 3, 2




Programming Exercises:

1) A certain computer science professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:0, <60:F. Write a program that accepts an exam score as input and uses a decision structure to calculate the corresponding grade.




2) A certain college classifies students according to credits earned. A student with less than 7 credits is a freshman. At least 7 credits are required to be a sophomore, 16 to be a junior and 26 to be classified as a senior. Write a program that calculates class standing from the number of credits earned.




3) A formula for computing Easter in the years 1982-2048, inclusive, is as follows: let a = year % 19, b = year % 4, c = year % 7, d = (19a + 24) % 30, e = (2b + 4c + 6d + 5) % 7. The date of Easter is March 22 + d + e (which could be in April) . Write a program that inputs a year, verifies that it is in the proper range, and then prints out the date of Easter that year.




4) A year is a leap year if it is divisible by 4, unless it is a century year that is not divisible by 400. (1800 and 1900 are not leap years while 1600 and 2000 are.) Write a program that calculates whether a year is a leap year.




5) Write a program that accepts a date (month, day, and year) and outputs whether or not the date is valid. For example 5/24/1962 is valid, but 9/31/2000 is not. (September has only 30 days.)




6) Write a program that reads three numbers and prints their median.




------------------------------------------------------------------------------------------------------

More products