Studying Logic Branching to Make a Python Program

Studying Logic Branching to Make a Python Program
For those of you new to learn programming will certainly ask:
What the heck is that branching and branching name why?
For you who have not been to college or learn about algorithms and flowcharts, perhaps for the first term you hear.
This term is to describe the flow of Python programs that form branching logic.
In the flow chart, following the logic of ” if … then ” depicted in the form of branches.
Studying Logic Branching to Make a Python Program
For this reason, it is called branching.
In addition to branching, these structures are also often referred to as flow control, decision, structural condition, structure if, etc.
Branching will be able to make a Python program thinking and decisive action in accordance with the logic / conditions that we provide.
On this occasion, you will learn the branching structure in python. Ranging from the most basic to the complex.
Make sure already know about relational operators and logical .

If branching structure

If branching is used when there is a choice decision.
For example, if we do not pass the exam, then we took remedial. Meanwhile, if passed does not need to participate remidi.
Studying Logic Branching to Make a Python Program
Then we can make an example of code themselves like this:

if passed == “no”:
print ( “You have to come remidi”)
“If you pass ==” no “then print the text” You have to come remidi “”

We use the same relationship with the operator (==) to compare the contents of the variable pass. Meanwhile, a colon (:) is the signal to start If the code block.
If the writing block, should be indented tabs or spaces 2 times.
❌ wrong Example:

if passed == “no”:
print ( “You must come remidi”)

Examples of writing the correct ✔️:

if passed == “no”:
print ( “You have to come remidi”)

Sample Coding Python Program

Now, let us apply to our program coding examples, Make the following Python program:

pass = raw_input ( “Did you graduate? [Yes / no]:”)
if pass == “no”:
print ( “You have to take the exam”)

Please executed, and then enter the answer “yes” or “no” .
Example coding again.
Let more understanding, please try the following example code program.
# program to check bonuses and discounts
# file:
total_shopping = input ("Total shopping: Rp")
# the amount to be paid is what the total expenditure is
# but if you get a discount it will decrease
pay = total_shopping
# if he is shopping above 100 thousand then give bonuses and discounts
if total_shopping>100000:
    print ("You get a cold drink bonus")
    print ("and 5% discount")
# calculate the discount
discount = total_shopping * 5/100 #5%
pay = total_shopping - discount
# print receipt
print ("Total to be paid: Rp %s" % pay)
print ("Thank you for shopping")
print ("Come again...")
The result if i input 1500000 for total shopping:
Studying Logic Branching to Make a Python Program

Branching structure If / Else

Branching If / Else is used when there are two selection decisions.
For example, if age is above or equals to 18 can make cards. While under it can not.
Then we can make it in a Python program is as follows:
age = input ("How old are you:")
if age>= 18:
     print ("You can make a SIM")
     print ("You may not create a SIM")

If the block addition, there is also the Else block to be executed if the condition age> = 18 one (False).

The result of the execution of the code above is as follows if we input below 18 years of age:
Studying Logic Branching to Make a Python Program

If branching structure / Elif / Else

If branching / Elif / Else is used when there are more than two choices results.

if so:
then the execution is
elif so:
then this execution
in addition to the above

Keywords elif has meaning Else if, its function is to create the conditions / additional logic if the first condition is wrong.
Program Python code then we can make as follows example:
# file
value = input ("Input your score:")
if value>= 90:
    grade = "A"
elif value>= 80:
    grade = "B +"
elif value>= 70:
    grade = "B"
elif value>= 60:
    grade = "C +"
elif value>= 50:
    grade = "C"
elif value>= 40:
    grade = "D"
    grade = "E"
print ("Grade:% s"% grade)
Results of coding if we input the value of 99 will get a grade A:

Studying Logic Branching to Make a Python Program

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan.

Previous post Mempelajari Percabangan untuk Membuat Logika Program Python
Next post Membuat Sertifikat dan Kunci Server untuk webMethods