If statement
If Statement
An else statement can be combined with an if statement. An else statement contains a block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at the most only one else statement following if.
If the statement is a VERY important tool for programming
If it's sunny
It's a sunny day
Wear your sunglasses
Otherwise, it's rainy
It's a rainy day
Get your umbrella
Otherwise
It's a great day in town
So we will program this statement. We will start with a boolean variable.
Now we will declare it False, and run it.. Nothing happens
Now let's switch again to True. And add another statement
Now let's add the else statement with True value. Maybe is not either sunny or rainy
Now look what happens when we change it to False because probably is cloudy..not rainy, nor sunny
You need to put a down payment 10% if you have good credit, otherwise, you need to put a downpayment of 20%
price = 30,000
has_good_credit = True
if has_good_credit :
down_payment= 0.1 * price
else:
down_payment = 0.2 * price
print(F"Down Payment :${down_payment}")
Now assume the value is True ( good credit)
Now assume it's False (bad credit)
Now let's add the else statement with True value. Maybe is not either sunny or rainy
Now look what happens when we change it to False because probably is cloudy..not rainy, nor sunny
Exercise
You want to buy a car and you went to a Toyota dealer.The car price is $20,000.You need to put a down payment 10% if you have good credit, otherwise, you need to put a downpayment of 20%
price = 30,000
has_good_credit = True
if has_good_credit :
down_payment= 0.1 * price
else:
down_payment = 0.2 * price
print(F"Down Payment :${down_payment}")
Now assume the value is True ( good credit)
Now assume it's False (bad credit)
If statement
Reviewed by ohhhvictor
on
May 04, 2019
Rating:
No comments: