Python with AI – I
Flow Chart
What are flow charts
• A flow chart represents the algorithm using a diagram
• Creating a flow chart helps design the logic of the program
Example – Add two numbers given by a user
Example – Add two numbers given by a user
Start
Accept first
input: num1
Example – Add two numbers given by a user
Start
Accept first
input: num1
Accept second
input: num2
Example – Add two numbers given by a user
Start
Accept first
input: num1
Accept second
input: num2
Sum = num1 + num2
Example – Add two numbers given by a user
Start
Accept first
input: num1
Accept second
input: num2
Add = num1 + num2
Print Add
Stop
Example – Add two numbers given by a user
Start
Accept first
input: num1
Accept first
input: num2
Add = num1 + num2
Print Add
Stop
Example – Add two numbers given by a user
Start
Accept first
input: num1
Accept first
input: num2
Add = num1 + num2
Print Add
Stop
Basic flow chart symbols
Symbol Purpose Description
Flow line Indicated the flow from one block
to another
Start/Stop Represents the start and end of the
flow chart
Processing Arithmetic operations
Input/Output Input and output operations
Conditional Decision making block
Example – Print the maximum between two
numbers Start
Accept first
number: num1
Accept second
number: num2
as input
Print num1
Stop
If
num1>
num2
Print num2
True False
Example – Print the maximum between two
numbers Start
Accept first
number: num1
Accept second
number: num2
as input
Print num1
Stop
If
num1>
num2
Print num2
True False
Verify that the flow chart
works correctly for a few
examples
num1 = 10, num2 = 20
num1 = 15, num2 = 5
Exercise: Create a flow chart for this problem
• Given 3 numbers, a, b, c, find the maximum value
Exercise: Create a flow chart for this problem
• Given 3 numbers, a, b, c, find the maximum value
• Verify that what your flow chart works correctly
• Examples:
• a = 10, b = 20, c = 30
• a = 15, b = 30, c = 5
• a = 40, b =80, c = 120
Exercise: Create code
Start
Declare 3
numbers: a, b, c
If a > b
Print b
FalseTrue
If a > c
Print a
If b > cPrint c
Stop
True True
False False
Flow Chart: Loops
• Ask user to enter a number
• Print all even numbers between 0 and the number user entered
Flow Chart: Loops
Start
User inputs a number num1
Flow Chart: Loops
Start
User inputs a number num1
a = 1
If a <=
num1
True Is a
divisible by
2?
Stop
True
Print a
False
Flow Chart: Loops
Start
User inputs a number num1
a = 1
If a <=
num1
True Is a
divisible by
2?
Stop
True
Print a
a = a+1
False
HW discussion
• Find if a number is prime, how would a flow chart for this look?
HW discussion
Start
User inputs a number num1
What is a prime number?
HW discussion
Start
User inputs a number num1
a = 1
If a <
num1
True Is num1
divisible by
a?
Print it’s a
prime number
Stop
True
Print it’s not a
prime number
a = a+1
How do you check if a string is a palindrome?
• How do you access different characters of a string?
How do you check if a string is a palindrome?
• How do you access different characters of a string?
• How do you access several characters in a string as a block?
How do you check if a string is a palindrome?
• How do you access different characters of a string?
• How do you access several characters in a string as a block?
• Please create a flowchart of how you would check if something is a
palindrome logically. Then use the above two operations to code the
logic.
Lets try it!
http://aiclub.world

Pa1 wednesday flow_chart

  • 1.
    Python with AI– I Flow Chart
  • 2.
    What are flowcharts • A flow chart represents the algorithm using a diagram • Creating a flow chart helps design the logic of the program
  • 3.
    Example – Addtwo numbers given by a user
  • 4.
    Example – Addtwo numbers given by a user Start Accept first input: num1
  • 5.
    Example – Addtwo numbers given by a user Start Accept first input: num1 Accept second input: num2
  • 6.
    Example – Addtwo numbers given by a user Start Accept first input: num1 Accept second input: num2 Sum = num1 + num2
  • 7.
    Example – Addtwo numbers given by a user Start Accept first input: num1 Accept second input: num2 Add = num1 + num2 Print Add Stop
  • 8.
    Example – Addtwo numbers given by a user Start Accept first input: num1 Accept first input: num2 Add = num1 + num2 Print Add Stop
  • 9.
    Example – Addtwo numbers given by a user Start Accept first input: num1 Accept first input: num2 Add = num1 + num2 Print Add Stop
  • 10.
    Basic flow chartsymbols Symbol Purpose Description Flow line Indicated the flow from one block to another Start/Stop Represents the start and end of the flow chart Processing Arithmetic operations Input/Output Input and output operations Conditional Decision making block
  • 11.
    Example – Printthe maximum between two numbers Start Accept first number: num1 Accept second number: num2 as input Print num1 Stop If num1> num2 Print num2 True False
  • 12.
    Example – Printthe maximum between two numbers Start Accept first number: num1 Accept second number: num2 as input Print num1 Stop If num1> num2 Print num2 True False Verify that the flow chart works correctly for a few examples num1 = 10, num2 = 20 num1 = 15, num2 = 5
  • 13.
    Exercise: Create aflow chart for this problem • Given 3 numbers, a, b, c, find the maximum value
  • 14.
    Exercise: Create aflow chart for this problem • Given 3 numbers, a, b, c, find the maximum value • Verify that what your flow chart works correctly • Examples: • a = 10, b = 20, c = 30 • a = 15, b = 30, c = 5 • a = 40, b =80, c = 120
  • 15.
    Exercise: Create code Start Declare3 numbers: a, b, c If a > b Print b FalseTrue If a > c Print a If b > cPrint c Stop True True False False
  • 16.
    Flow Chart: Loops •Ask user to enter a number • Print all even numbers between 0 and the number user entered
  • 17.
    Flow Chart: Loops Start Userinputs a number num1
  • 18.
    Flow Chart: Loops Start Userinputs a number num1 a = 1 If a <= num1 True Is a divisible by 2? Stop True Print a False
  • 19.
    Flow Chart: Loops Start Userinputs a number num1 a = 1 If a <= num1 True Is a divisible by 2? Stop True Print a a = a+1 False
  • 20.
    HW discussion • Findif a number is prime, how would a flow chart for this look?
  • 21.
    HW discussion Start User inputsa number num1 What is a prime number?
  • 22.
    HW discussion Start User inputsa number num1 a = 1 If a < num1 True Is num1 divisible by a? Print it’s a prime number Stop True Print it’s not a prime number a = a+1
  • 23.
    How do youcheck if a string is a palindrome? • How do you access different characters of a string?
  • 24.
    How do youcheck if a string is a palindrome? • How do you access different characters of a string? • How do you access several characters in a string as a block?
  • 25.
    How do youcheck if a string is a palindrome? • How do you access different characters of a string? • How do you access several characters in a string as a block? • Please create a flowchart of how you would check if something is a palindrome logically. Then use the above two operations to code the logic.
  • 26.