Setelah lulus assessment (Module 06) dengan nilai 96%, maka kita masuk ke Module 07 tentang Making Decision with code.
Silahkan masuk ke Module 07: Making Decisions with Code | Learning Outcomes | Alison
Kita mulai dengan topik if statement (part 1).
if answer == "yes"
answer = input("would you like express shipping?")
if answer == "yes":
print("will be an extra $10")
print("have a nice day")
Run python file:
would you like express shipping?yes
will be an extra $10
have a nice day
if answer == "no"
answer = input("would you like express shipping?")
if answer == "yes":
print("will be an extra $10")
print("have a nice day")
Run python file:
would you like express shipping?no
have a nice day
if answer == "yes".....> 3 print, dengan 1 print un-indent
answer = input("would you like express shipping?")
if answer == "yes":
print("will be an extra $10")
print("but expensive for me")
print("have a nice day")
Run python file:
would you like express shipping?yes
will be an extra $10
but expensive for me
have a nice day
Note: Python is "case" sensitive----> perhatikan huruf besar atau kecil.
Continue to if statement part 2: If Statements - Part II - Online Course | Alison
Deposit = 150
deposit = 150
if deposit > 100:
print("You get a free toaster")
print("Have a nice day")
Run python file:
You get a free toaster
Have a nice day
Deposit = 50
deposit = 50
if deposit > 100:
print("You get a free toaster")
print("Have a nice day")
Run python file:
Have a nice day
Deposit = int(input)
deposit = int(input("how much do you want to deposit? "))
if deposit > 100:
print("You get a free toaster")
print("Have a nice day")
Run python file:
how much do you want to deposit? 125
You get a free toaster
Have a nice day
Continue to Branching: Branching - Online Course | Alison
If deposit > 100
deposit = float(input("how much to deposit? "))
if deposit > 100:
print("you get a free toaster")
else:
print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 110
you get a free toaster
have a nice day
if deposit < 100
deposit = float(input("how much to deposit? "))
if deposit > 100:
print("you get a free toaster")
else:
print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 95
you get a mug
have a nice day
if deposit = 100
deposit = float(input("how much to deposit? "))
if deposit > 100:
print("you get a free toaster")
else:
print("you get a mug")
print("have a nice day")
Run python file:
how much to deposit? 100
you get a mug
have a nice day
# Boolean variable: True or False
Deposit > 100
deposit = int(input("how much to deposit? "))
if deposit > 100:
freeToaster = True
if freeToaster:
print("Enjoy your Toaster")
print("have a nice day")
Run python file:
how much to deposit? 110
you get a free toaster
have a nice day
Jika kita letak deposit < 100, maka akan terjadi:
error: NameError: name 'freeToaster' is not defined
Maka, kita harus tambah coding: freeToaster = False di atas.
freeToaster = False
deposit = int(input("how much to deposit? "))
if deposit > 100:
freeToaster = True
if freeToaster:
print("Enjoy your Toaster")
print("have a nice day")
Run python file:
how much to deposit? 80
you get a mug
have a nice day
Constraints for checking your if statements:
== is equal to
!= is not equal to
< is less than
> Is greater than
<= is less than or equal to
>= is greater than or equal to
== is equal to
!= is not equal to
< is less than
> Is greater than
<= is less than or equal to
>= is greater than or equal to
When checking two strings are equal to each other with an if statement, it is often best to convert both of them to upper or lower case, as python is case sensitive and will not properly work less both are the same case.
There are generally two different ways to write an if statement you should always consider which method makes the most sense and is the simplest.
Continue: Module 8: Complex Decisions with Code