If Statements Examples- Python VII
Interactive Python Activities
Example 1: Grading System
Write a Python program to calculate the grade of a student based on their marks. The program should prompt the user to input a numerical grade (0–100) and determine the letter grade using the following ranges:
- 90–100: Grade A
- 80–89: Grade B
- 70–79: Grade C
- 60–69: Grade D
- Below 60: Grade F
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")
Hint: Use conditional statements to evaluate different ranges of marks.
Example 2: Calculating Discounts
Write a Python program to calculate the final price of an item after applying a discount. The program should use the following discount rules:
- 10% discount if the amount is greater than $1000
- 5% discount if the amount is greater than $500 but less than or equal to $1000
- No discount if the amount is $500 or less
if amount > 1000:
discount = amount * 0.1
elif amount > 500:
discount = amount * 0.05
else:
discount = 0
print(f"Total discount: {discount}")
Hint: Check the amount against different thresholds and apply the corresponding discount.
Example 3: Checking Eligibility to Vote
Write a Python program to determine whether a person is eligible to vote. The program should ask the user to input their age and check if it meets the minimum voting age requirement (18 years or older).
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Hint: Use a simple condition to check if age is greater than or equal to 18.
Comments
Post a Comment