Conditional statements are ones that are executed only if some condition is true. In this example, the interestRate is set higher if the principal is large.
interestRate = .1 # the default principal = input ('please enter the amount in your account') if principal > 10000:
An else clause says what to do if the 'if' condition is false, e.g.,
if principal > 10000:
else
interestRate = .1
If you have more than two alternatives, use the keyword elif, which is short for else-if.
if principal > 10000: interestRate=.2
elif principal > 8000:
elif principal > 6000:
interestRate = .13
else:
interestRate = .1
For this sample, when will interest rate be .15? When will it be .1?You can have as many 'elif's as you want.
Comparison and Logical OperatorsHere are examples of the comparison operators that you can use in conditionals: if principal < 3: if principal > 3 principal == 3 principal != 3 principal <= 3 principal >= 3
Note
that == is used for equality comparison as opposed to = (the assignment
operator). This causes a lot of errors even for experienced
programmers. != means not equal.
You can also use the logical operators and, or, and not, to create more complex conditions, e.g.,
if not (principal>100 and principal<300 ): print 'principal is less than 101 or bigger than 299'
if (principal>3000 or numYears>5): print 'principal is large or you've had an account for a long time'
Statement BlocksOften,
in a conditional statement, you want more than one thing to occur.
Programming languages allow you to define a statement block, which is
one or more statements that are grouped together. In many languages,
such as C, C++, and Java, curly braces are used to denote statement
blocks:
if <condition> {
<statement> <statement> ...
}
In
such languages, indentation is ignored by the compiler. Python is
different. It uses indentation to define where a block begins and ends
and doesn't use curly brackets. Tabs are used to indent:
if <condition> :
<statement> <statement> ...
We say that the indented statements are 'subordinate' to the 'if-condition'-- they are only executed if the condition is true. The next statement that is not indented ends the block and is executed no matter what.
In this way, conditional if statements are similar to iterative while statements. The difference is that, when the statements within a while loop complete, you loop back up and check the condition again.
In-Class Worksheet
1. Suppose a bank has the following rules for its accounts. If the principal is less than $10,000, the interest rate is 10% (.1). If it is greater than or equal to $10,000, the interest rate is 20%. If it is over $50,000, the rate is 25%.
Write a program which asks the end-user to enter the principal and then prints out the new principal after one and two years. If you have an interest rate program already, copy it into conditionalInterest.py and make the necessary changes.
2. Suppose the bank has the rules above and on more: after the money is in the bank a third year, customers receive 30% no matter what the principal.
Write a program that
will ask the user for a principal and the number of years, and then prints out the final principal after interest has been earned for the given number of years.
You may begin this program by copying your program from (1) into interestAfterYears.py.
|
|