Thursday, February 20, 2025

Diploma Python Programming - Module 08 - Alison 03

Sekarang, kita lanjutkan pelajaran ke Module 8, dimana topiknya berkenaan dengan "complex Decision with Code."


Image - Python programming (credit to Google).


elif country ---> Germany
country = input("where are you from? ")
if country == "Canada" :
    print("Hello")
elif country == "Germany" :
    print("Guten Tag")
elif country == "France" :
    print("Bonjour")
else:
    print("aloha/ciao/apa kabar")
Run Python file:
where are you from? Germany
Guten Tag

else country
country = input("where are you from? ")
if country == "Canada" :
    print("Hello")
elif country == "Germany" :
    print("Guten Tag")
elif country == "France" :
    print("Bonjour")
else:
    print("aloha/ciao/apa kabar")
Run Python file:
where are you from? Indonesia
aloha/ciao/apa kabar


Combining Conditions
If first AND second condition:
True AND True ...> True
True AND False ...> False
False AND True ...> False
False AND False ...> False

wonLottery AND bigwin.
wonLottery = True
bigwin = True
if wonLottery and bigwin :
    print("you can retire")
Run Python file:
you can retire

if, elif and else:
team = input("enter your favorite team: ").upper()
sport = input("enter your favorite sport: ").upper()

if team == "FLYER":
    print("best team ever!")
elif team == "SENATOR":
    print("Go Senator Go")
else:
    print("I don't have anything to say")
Run Python file:
enter your favorite team: SENATOR
enter your favorite sport: BARCELONA
Go Senator Go


Combining Conditions
If first OR second condition:
True OR True ...> True
True OR False ...> True
False OR True ...> True
False OR False ...> False

if Saturday or Sunday
saturday = True
sunday = False
if saturday or sunday:
    print("you can sleep in")
Run Python file:
you can sleep in


sampai di sini, 16.00: Combining Conditions: Combining Conditions - Online Course | Alison
WonLottery = True
BigWin = True

if WonLottery and BigWin
    print("you can retire")

if first condition AND second condition
if first condition    second condition    statement is
True                        True                            True
True                        False                           False
False                       True                            False
False                        False                          False

team = input("Enter your favorite hockey team: ").upper()
sport = input("Enter your favorite sport: ").upper()

if sport == "HOCKEY" and team == "RANGERS":
    print("I miss Messier")
else: 
    print("I don't know that team)
---------------------------


Nested if Statements
Your challenge
Please use python language with Nested if statements:
- Calculate total to charge for an order from an online store in the USA
- ask user what country they from and their order total
country = input("which country are you from?")
order_total = float(input("what's your order total?")

# Initialize the total charge with the order total
total_charge = order_total


- if the user from USA, ask which state
if country.lower() == "usa":
    state = input("Which state are you from? ")

- if the order is from outside USA do not charge any taxes
- If the order was placed in USA, calculate tax based on states: Louisiana charge 1% sales taxes, all other states charge 3% sales tax.
 if state.lower() == "louisiana":
        tax_rate = 0.01  # 1% sales tax for Louisiana
    else:
        tax_rate = 0.03  # 3% sales tax for all other states

 # Calculate the total charge including tax
    total_charge += order_total * tax_rate
else:
    # No tax for orders outside the USA
    tax_rate = 0.00

# Print the total charge
print(f"Your total charge is: ${total_charge:.2f}")
--------------------------


# Ask the user for their country and order total
country = input("Which country are you from? ")
order_total = float(input("What's your order total? "))

# Initialize the total charge with the order total
total_charge = order_total

# Check if the user is from the USA
if country.lower() == "usa":
    state = input("Which state are you from? ")
    
    # Check if the state is Louisiana
    if state.lower() == "louisiana":
        tax_rate = 0.01  # 1% sales tax for Louisiana
    else:
        tax_rate = 0.03  # 3% sales tax for all other states
    
    # Calculate the total charge including tax
    total_charge += order_total * tax_rate
else:
    # No tax for orders outside the USA
    tax_rate = 0.00

# Print the total charge
print(f"Your total charge is: ${total_charge:.2f}")
------------------

Notes 1:
# Initialize a variable
total = 10

# Use the += operator to add 5 to the total
total += 5  # This is equivalent to total = total + 5

# Print the result
print(total)  # Output: 15


Notes 2:
meaning: print(f"Your total charge is: ${total_charge:.2f}")

The line print(f"Your total charge is: ${total_charge:.2f}") is using an f-string, which is a way to format strings in Python introduced in version 3.6.

Here’s a breakdown of what it does:

  • f"...": This indicates that the string is an f-string. It allows you to embed expressions inside curly braces {} that are evaluated at runtime.

  • Your total charge is:: This is a literal string that will be printed as is.

  • ${total_charge:.2f}: Inside the curly braces, you have the variable total_charge, which is formatted to two decimal places. The .2f specifies that you want to format the number as a float with two digits after the decimal point.

So, if total_charge were 10.456, the output would be: 10.46
-------------------



JJJJJJJJJJJJJJJJJJJJ
sampai di sini, 16.00: Combining Conditions: Combining Conditions - Online Course | Alison


JJJJJJJJJJJJJJJJJJJJJJ