Skip to content

2. Conditional Expressions

Barak Haim edited this page Dec 1, 2020 · 7 revisions

Conditional Expressions - If/Else Statements

If Statement

if <condition>:
    <code>
<back to flow>

Side note - Indentation in python

Indentation is mandatory for the interpreter in order to understand the workflow.

If/Else Statements

if <condition>:
    <code>
else:
    <other code>
<back to flow>

If/elif/Else Statements

if <condition>:
    <code>
elif <condition>:
    <code>
else:
    <other code>
<back to flow>

Multiple if Statements

if <condition>:
    <code>
if <condition>:
    <code>
else:
    <other code>
<back to flow>

Nested if Statements

if <condition>:
    <code>
    if <condition>:
        <code>
    else:
        <other code>
else:
    <other code>
    if <condition>:
        <code>
    else:
        <other code>
<back to flow>

Pass Statement

if <condition>:
    Pass
if <condition>:
    <code>
else:
    <other code>
<more code>

One-liners

Inline Ifs

if <condition>: <statement_1>; <statement_2>; ...; <statement_n>

Conditional Expressions

a = <expr1> if <condition> else <expr2>
#Example:
n = 1 if 1<2 else 2
# now n is 1
n = 1 if 1>2 else 2
# now n is 2

More info.

Exercise

  1. Prompt user for a number between 0-100. Tell them they won the game if they choose less then 29. Lost it otherwise. Assume the input is really between 1-100.
  2. Prompt user for an integer > 0. This number defines a spectrum. Ask the user to guess a number within the spectrum. They win iff they are in the bottom 10 percent of the spectrum or in the top 10. Anything in between - they lose. Assume the input is valid.
  3. Following #2 - before you play they game, check if the 2nd number is within the spectrum. If not, tell them we don't play if they try to cheat.
  4. Following #3 - Make sure the users give proper integers before starting the game.
  5. Following #4 - Lets give losers a 2nd chance. If they can guess the spectrum's average we'll let them win anyway but only in the condition they guess a valid integer.
  6. Following #5 - Here's a list of ID's: 59, 61, 67, 71, 73, 79, 83, 89, 97. Those people have tried to cheat us before. Before playing the game, ask the users for their name and ID. If the ID is not valid (valid IDs are 2 digit numbers) or, if this user tried to cheat us before (we assume they can't give a fake ID) - kick them out! If all is "Kosher" - greet them by their name and start the game.
  7. Write a tax calculator. We'll make an example of a differential taxing system common in the world (a similar system is used in Israel). If a worker's monthly salary is up to 100, there is no tax. if the salary is up to 1000, the first 100 is free of tax and the rest is taxed 10%. if the salary is up to 10000, the first 100 is free of tax, the rest up to 1000 is taxed 10% and the rest is taxed 20%. if the salary is up to 100000, the first 100 is free of tax, the rest up to 1000 is taxed 10%, the rest up to 10000 is taxed 20% and the rest is taxed 30%. Every earnings larger then 100000 are taxed 50%. Prompt the users for their monthly salary and print out the tax they have to pay and the NET salary. These kinds of programs are used in the real world.

Clone this wiki locally