Real-Time Example Program Using if, elif, else Conditions

Question 1:

Get input for score out of 100 and evaluate performance:

If the score is less than 35, print "Poor Student".

If the score is greater than 35 but less than 70, print "Average Student".

If the score is greater than 70, print "Good Student".


Program:


# Get input for score out of 100
Score = int(input("Score: "))

if Score < 35:
    print("Poor Student")
elif Score >= 35 and Score < 70:
    print("Average Student")
elif Score >= 70:
    print("Good Student")
else:
    print("Invalid Score")


Output Example:


Score: 30
Poor Student

Score: 65
Average Student

Score: 85
Good Student


Program Step-by-Step Explanation:

Step 1: Input the score

  • The program asks the user to enter a score between 0 and 100.

Step 2: Check if the score is less than 35

  • If the score is less than 35, it prints "Poor Student."

Step 3: Check if the score is between 35 and 70

  • If the score is between 35 and 70, it prints "Average Student."

Step 4: Check if the score is 70 or higher

  • If the score is 70 or higher, it prints "Good Student."

Step 5: Handle invalid scores

  • If the score is not within the expected range (0-100), the program prints "Invalid Score."






Question 2:

Create a mini calculator that gets input for two numbers a and b, and an operation (add/sub/mul/div). If the user selects an operation, the program performs it and prints the result.



Program:


a = int(input("Enter the number A: "))  
b = int(input("Enter the number B: ")) 

operation = input("add/sub/mul/div: ")

if operation == "add":  
    print("Result:", a + b)
elif operation == "sub": 
    print("Result:", a - b)
elif operation == "mul":  
    print("Result:", a * b)
elif operation == "div": /
    print("Result:", a / b)
else: 
    print("Invalid operation")
                


Output Example:


Enter the number A: 5
Enter the number B: 6
add/sub/mul/div: add
Result: 11
                


Program Step-by-Step Explanation:

Step 1: Input numbers

  • The program asks for two numbers (a and b).

Step 2: Input operation

  • The user selects an operation (add, sub, mul, or div).

Step 3: Perform the operation

  • Based on the chosen operation:
    • add → Adds a and b.
    • sub → Subtracts b from a.
    • mul → Multiplies a and b.
    • div → Divides a by b.

Step 4: Output result

  • The result of the operation is printed.
  • If the operation is invalid, an error message is shown.

Post a Comment

0 Comments