Section 3.9 - IT 140_ Introduction to Scripting _ zyBooks

pdf

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

140 - X625

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

3

Uploaded by DeaconCaterpillar4154

Report
Students: Section 3.9 is a part of 1 assignment: 3-1 zyBooks Participation Activities Includes: PA 3.9 Conditional expressions Conditional expressions A conditional expression has the following form: Construct 3.9.1: Conditional expression. expr_when_true if condition else expr_when_false All three operands are expressions. The condition in the middle is evaluated ±rst. If the condition evaluates to True, then expr_when_true is evaluated. If the condition evaluates to False, then expr_when_false is evaluated. For example, if x is 2, then the conditional expression 5 if x==2 else 9*x evaluates to 5. A conditional expression has three operands and thus is sometimes referred to as a ternary operation . Good practice is to restrict usage of conditional expressions to an assignment statement, as in: y = 5 if (x == 2) else 9*x . Some Python programmers denounce conditional expressions as di²cult to read and comprehend, since the middle operand is actually the ±rst evaluated, and left-to-right syntax is preferred. However, simple assignments such as the statement above are acceptable. PARTICIPATION ACTIVITY 3.9.1: Conditional expression. PARTICIPATION ACTIVITY 3.9.2: Conditional expressions. Convert each if-else statement to a single assignment statement using a conditional expression, using parentheses around the condition. Enter "Not possible" if appropriate. 1) if x < 100 : y = 0 else : y = x y = 0 if (x < 100) else x 2) if x < 0 : x = - x else : x = x x = -x if (x < 0) else x 3) if x < 1 : y = x else : z = x Not possible CHALLENGE ACTIVITY 3.9.1: Conditional expression: Print negative or non-negative. Create a conditional expression that evaluates to string "negative" if user_val is less than 0, and "non-negative" otherwise. Feedback? if condition : my_var = expr1 else : my_var = expr2 my_var = if ( condition ) else expr1 expr2 1. This if-else form can be written as a conditional expression. 2. The condition in the middle is evaluated ±rst. 3. If the condition evaluates to True, then expr1 is evaluated and its value is assigned to my_var. 4. If the condition evaluates to False, then expr2 is evaluated and its value is assigned to my_var. Captions Feedback? Check Show answer Correct y = 0 if (x < 100) else x or y = 0 if x < 100 else x The condition is "(x < 100)". The expression when true is "0". The expression when false is "x". Check Show answer Correct x = -x if (x < 0) else x or x = -x if x < 0 else x The condition is "(x < 0)". The expression when true is "-x". The expression when false is "x". Check Show answer Correct Not possible The if branch assigns y, while the else branch assigns z, so this cannot be converted to a conditional expression. Feedback? If the condition evaluates to False, then expr2 is evaluated and its value is assigned to my_var. 1 2 3 4 2x speed
Sample output with input: -9 -9 is negative Learn how our autograder works 553398.3976864.qx3zqy7 All tests passed Testing with input: -9 Your output -9 is negative Testing with input: 0 Your output 0 is non-negative Testing with input: 3735 Your output 3735 is non-negative CHALLENGE ACTIVITY 3.9.2: Conditional expression: Conditional assignment. Using a conditional expression, write a statement that increments num_users if update_direction is 3, otherwise decrements num_users. Sample output with inputs: 8 3 New value is: 9 Learn how our autograder works 553398.3976864.qx3zqy7 All tests passed Testing with inputs: 8 3 Your output New value is: 9 Testing with inputs: 8 0 Your output New value is: 7 Testing with inputs: 6 0 Your output New value is: 5 Testing with inputs: 5 2 Your output New value is: 4 How was this section? | user_val = int ( input ()) cond_str = "negative" if user_val < 0 else "non-negative" print ( user_val , 'is' , cond_str ) Run Feedback? num_users = int ( input ()) update_direction = int ( input ()) num_users = ( num_users + 1) if update_direction == 3 else ( num_users - 1) print ( 'New value is:' , num_users ) Run Feedback? Provide section feedback 1 2 3 4 5 1 2 3 4 5 6
Activity summary for assignment: 3-1 zyBooks Participation Activities 100 % 100 % submitted to desire2learn Completion details
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help