Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.7-P2 Nested Conditionals
Student led teaching Nested Conditionals using Javascript and Python
Pseudo Code Python and Java
Nested conditionals expand on the If-Then-Else principles introduced in Lesson 3.7.
- Conditional Statements: Execute different actions depending on specific conditions.
- Nested Conditionals: A conditional statement placed within another conditional statement.
Python Example Code
score = int(input("Enter the score: "))
if score >= 90:
print("A")
else:
if score >= 80:
print("B")
else:
if score >= 70:
print("C")
else:
if score >= 60:
print("D")
else:
print("F")
Javascipt
%%js
let score = parseInt(prompt("Enter the score:"));
if (score >= 90) {
console.log("A");
} else {
if (score >= 80) {
console.log("B");
} else {
if (score >= 70) {
console.log("C");
} else {
if (score >= 60) {
console.log("D");
} else {
console.log("F");
}
}
}
}
<IPython.core.display.Javascript object>
Credit Score Example
credit_score = 100
income = 10000
if credit_score < 700:
print("Not eligible for loan due to low credit score")
elif income < 50000:
print("Not eligible for loan due to low income")
else:
print("Eligible for loan")
Not eligible for loan due to low credit score
Javascript
%%js
let credit_score = 100;
let income = 10000;
if (credit_score < 700) {
console.log("Not eligible for loan due to low credit score");
} else if (income < 50000) {
console.log("Not eligible for loan due to low income");
} else {
console.log("Eligible for loan");
}
<IPython.core.display.Javascript object>
Example 3
Write pseudocode to determine if a person qualifies for a discount based on their membership status and purchase amount
- if the person is a member:
- If someone is a member and spends more than $100, they get a 20% discount. If they spend $100 or less, they get a 10% discount.
- If they’re not a member and spend more than $100, they get a 5% discount. If they spend $100 or less, they don’t get any discount.
Define is_member and purchase_amount
is_member = True # Set to True if the person is a member, False if not purchase_amount = 120 # Replace with the actual purchase amount
-
Both snippets (Python and JavaScript) check loan eligibility based on two factors: credit_score and income.
- If the credit_score is less than 700, both codes print a message stating that the person is not eligible due to a low credit score.
- If the credit_score is 700 or higher but the income is less than 50,000, both codes output that the person is not eligible due to low income.
- If both conditions are met (credit score ≥ 700 and income ≥ 50,000), both snippets print that the person is eligible for a loan.
- The Python code uses print() to display messages in the console, while the JavaScript code uses console.log() to achieve the same result.