Python Training

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Python Training

Prepared by: Eleanor Cheah


Example 1:
You follow your parents to a buffet restaurant. You saw the rules of the
restaurant at the entrance as shown below:
Price list:

Adult RM40
If 65 year old and above RM25

Kids (4 – 12 years old) RM20


if taller than 150 cm RM30

Below 4 years old FREE


If taller than 100 cm RM10

Write an algorithm for this restaurant to determine the price of one customer in
the form of:
1. Pseudocode
2. flowchart
Study the below scenarios:
• Scenario 1:
Ahmad runs in a marathon. When he reaches the finishing line, if there is
nobody reaches before him, he is the winner. If at the same time, he can break
the record, not only he will take away the champion cup, he will also win a cash
reward. If he is not the first that reaches the finishing line, he will receive
certificate of participation.
• Scenario 2:
Ahmad runs in a marathon. So long as he doesn’t see the finishing line, he needs
to keep running.

Discuss among your group and decide what structural statement you
will use for the above scenarios. Explain your choice.
Let’s conclude your finding.
• Scenario 1:
We use “if…else” statement.
• Scenario 2:
We use “while”

We want to focus on Scenario 2.


• In Form 1, we learned “while”.
• Today, we want to called it “while-do”
• “while-do” means, while the condition is not fulfilled, do (process
statement)
• Example, While Ahmad has not reach the finishing line, keep
running.
Let’s study another example:
Nadya likes to eat cookies. If given a jar, she eats all the cookies one at a time.

Assume there are 10 cookies in the jar, write an algorithm (both


pseudocode and flowchart) for this example.

Pseudocode provides a way of explaining an


algorithm without it being tied to a specific
programming language.
Flowcharts are a great way to plan a program if
you know you’re going to need looping, iteration
or selection.
• Do not memorize any of the codes in programming
• Read and try to understand what is given and what is asked in the
question, then write your own codes.
Sample outcome:

• Now let’s code the same example in Python.


• The syntax for “while”:
while condition: Colon is important.
statement

Small The statement for


letter. “while” should be
indented.
To check if the
programme gives the
desired outcome, we can
count the number of this
two print statements.
• Sample answer:
• Write a program using while loop, which prints the sum of every third
numbers from 1 to 1001 (both 1 and 1001 are included).
Example:
(1+4+7+10+…)
Given a bookshelf, for every book on the shelf, write the title of the
book
• Combine “for” with “list”
My_books=[“Python”, “Java”, “C++”, “Ruby”]
For x in my_books:
Print(x)
Print (“No more books on the shelf.”)
Print “hello” 10 times
My_numbers=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
For current_number in my_numbers :
Print (“Hello”)
For current_number in range(1,11):
print(“Hello”)
Modify challenge 2 by using for loop
Modify challenge 4 by using for loop
Name Test1 Test2 Test3

Bunny 90 87 95

Duck 78 96 89

Rubble 83 85 92
Continue
• The continue statement is used to skip the rest of the code inside a
loop for the current iteration only.
• Loop does not terminate but continues on with the next iteration.
Break
• The break statement terminates the loop containing it. The program
jumps to the statement immediately after the body of the loop.

You might also like