Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active March 7, 2017 07:37
Show Gist options
  • Save prodeveloper/a822dea2ad427dd502553d120b2e06f2 to your computer and use it in GitHub Desktop.
Save prodeveloper/a822dea2ad427dd502553d120b2e06f2 to your computer and use it in GitHub Desktop.
Working with if statements in python

We make choices in everyday life. Each choice takes us to a different branch of steps.

For example, you can chose to come by Matatu or by an Uber. Each choice has steps that follow naturally from it.

Computers can also be programmed to make choices. In python, this is done by using the if, elif* and else key words.

if some_true_value:
        ##Run some series of steps 
elif some_other_true_value:
        #Run if the first value to be tested was false, but this one is true
else:
        #Default option

See the reference on boolean values for a better understanding.

##Step 1

Let us suppose that you have been building an app for a restaurant. One of the features of the app is that it send out thank you messages when certain behaviours.

A requirement is thus proposed as:

    As a customer, I want to receive a thank you message "Thank you for your generous tip" if I tip more than Kshs 1000.

How would you go about that?

mkdir 7lesson
cd 7lesson
touch big_tipper.py

Open the newly created big_tipper.py and put in the code.

tip = 700

if tip > 1000:
    print("Thank you for the big tip")
    
print ("Thank you for dining with us")

What do you see when you run the file?

python3 big_tipper.py

What about when you change the variable tip from 700 to 7000. tip = 7000

What do you see?

Let us now acknowledge not only the big tippers but also the small tippers.

Now you have the requirement.

    As a customer, I want to receive a thank you message "Thank you for your generous tip" if I tip more than Kshs 1000.
    As a customer I want to receive a thank you message "Thank you for your tip, all help is appreciated" if I tip 1000 or less

Change your code to now be.

tip = 1100

if tip > 1000:
    print("Thank you for the big tip")
elif tip <= 1000:
    print("Thank you for your tip, all help is appreciated")
print ("Thank you for dining with us")

##Step 2

We are now able to check for simple conditionals, what if we however wanted our script to grade exams.

As a teacher, I want to give the marks a student got and get their grade. The grade scheme is 90 or above gets an A ,70 - 89 is a B, 50 - 69 is a C, anything below 50 is a D

In this scenario we now have to have multiple ifs.

Create a new file called grader.py

touch grader.py

Inside that file type in the code

def grader(grade):
    grade = int(grade)
    if grade >= 90:
        return "A"
    elif grade >=70:
        return "B"
    elif grade >= 50:
        return "C"
    else:
        return "D"
        
user_input = input("What is the students marks?:")
computed_grade = grader(user_input)
print("The students grade is {}".format(computed_grade))

Try running the code. Put in the different grades.

##Assignment

Write a script that takes in a students age and then groups then tells you if they are in primary, secondary or tertiary. All students below 13 years are in primary, all between 13 and 18 are in secondary, all above 18 are in tertiary.

##Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment