Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 94 - 2629_Devagiri_C-7
P. 94

    ∑  If equal or more than 50, show “Fairly Good”
                                     ∑  Otherwise, show “Need more effort”

                          Program6.py                                                     Output
                       File  Edit  Format   Run    Options   Window    Help            Enter your marks: 90

                       marks =int(input("Enter your marks:  "))                        Excellent
                       if marks>=90:                                                   Enter your marks: 75
                           print("Excellent")
                                                                                       Very Good
                       elif marks>=70:
                                                                                       Enter your marks: 51
                           print("Very Good")
                                                                                       Fairly Good
                       elif marks>=50:
                           print("Fairly Good")                                        Enter your marks: 40
                       else:                                                           Need more effort
                           print("Need more effort")

                      The number of elif statements used in this construct can vary depending on the problem and
                      there is no fixed upper limit for it. However, else can be used only once for each if statement.

                                                                                                    21 st
                           EXPERIENTIAL LEARNING                                                   Century   #Critical Thinking
                                                                                                    Skills

                        Create a Discount Calculator program that calculates the discount based on the purchase amount.
                        The discount rate changes based on the amount spent and the program should display the discount
                        amount and the final total. Students will use if-else statements to apply the correct discount and
                        perform mathematical operations like multiplication and subtraction based on the purchase value.

                       RAPID RECALL                             Tick ( ) if you know this.

                          1.  The if…else… statement lets your program choose between two options.

                          2.  The nested if structure allows you to place one if statement inside another.

                             TERNARY OPERATOR IN PYTHON

                      The ternary operator allows you to perform conditional checks and assign values or execute
                      expressions  in a single line. It is also called a  conditional  expression because  it  evaluates  a
                      condition and returns one value if the condition is True and another value if the condition is False.

                      Syntax of the ternary operator:

                      <value_if_true> if (condition) else <value_if_false>

                       92
                            Premium Edition-VII

| 89 90 91 92 93 94 95 96 97 98 99 |