# Start writing code here...
# example from Telco
phone_balance = 5
wallet_balance = 150 # paypal, etc.
threshold = 10
top_up_amount = 20
if phone_balance < threshold: # skipped if the condition is false
phone_balance += top_up_amount
wallet_balance -= top_up_amount
print(phone_balance)
print(wallet_balance)
25
130
# binary condition
number = 20
if number % 2 == 0: # this executes if the condition is True
print("The number is even.")
else: # this executes if the condition above is false.
print("The number is odd.")
The number is even.
# multiple conditions
language = "de" # German ("Deutsch")
language = "en" # English ("English")
#language = "zh" # Chinese
#language = "in" # Indonesian
# we can use the if else syntax,
# but this involves nested indentation
if language == 'de':
print("Hallo, wie geht es dir?")
else:
if language == 'en':
print('Hello, how are you?')
else:
if language == "zh":
print("哈罗, 你好吗?")
else:
if language == "in":
print("Hai, apa kabar?")
else:
print("Sorry, language is not supported.")
Hello, how are you?
language = "de" # German ("Deutsch")
language = "en" # English ("English")
#language = "zh" # Chinese
#language = "in" # Indonesian
if language == "in":
print("Hai, apa kabar?")
elif language == "zh":
print("哈罗,你好吗?")
elif language == "en":
print("Hello, how are you?")
elif language == "de":
print("Hallo, wie geht es dir?")
else:
print("Sorry, language is not supported.")
Hello, how are you?
# logical operator is used to invert a Boolean value
not True
# comparison operator is used to compare numbers, strings, etc.
"a" != "b"
%run -i students.py
Unsupported output type: clearOutput
The chosen one is: Natalia Chan
# Guessing Game
# Your task is to create a game where you are hidingg a number from your friend.
# Your friend (opponent) provides a guess.
# If the guess is lower than your number, then print "Sorry, your guess is too low"
# If the guess is higher than your number, then print "Sorry, your guess is too high"
# If the guess matches your number, print "Well done! Your guess is correct."
# TODO: define some variables, i.e. the number and the guess
number = 5
guess = 5
# TODO: write an if statement to print the messages as specified above
if guess < number:
print("Sorry, your guess is too low")
elif guess > number:
print("Sorry, your guess is too high")
else:
print("Well done! Your guess is correct.")
Well done! Your guess is correct.
# Task: write an if statement that states which prize the participants get
# If someone wins a medal, the message should state:
# "Congratulations! You won a [medal name]"
# If there is no prize, the message should state:
# "Sorry, no prize this time for you."
# POINTS | Prize
# 1 - 50 | No Prize
# 51 - 150 | Bronze Medal
# 151 - 180 | Silver Medal
# 181 - 200 | Gold Medal
# All the upper and lower bounds are inclusive.
points = 24 # you can assume that this variable is always between 0 - 200.
# TODO: write an if statement
# You can use the format() method if you wish.
if points < 51:
print("Sorry, no prize this time for you.")
elif points < 151:
print("Congratulations! You won a bronze medal.")
elif points < 181:
print("Congratulations! You won a silver medal.")
else:
print("Congratulations! You won a gold medal.")
Sorry, no prize this time for you.
gym_members = {"Joey": {'weight': 64, 'height':1.72, 'is_member': True, 'is_subscribed': True},
"Antonio": {'weight': 78, 'height': 1.78, 'is_member': True, 'is_subscribed': True}}
height = gym_members['Antonio']['height']
weight = gym_members['Antonio']['weight']
is_member = gym_members['Antonio']['is_member']
is_subscribed = gym_members['Antonio']['is_subscribed']
if 18.5 <= weight/height**2 < 25:
print("BMI is normal.")
if is_member and is_subscribed:
print("Member receives newsletters.")
BMI is normal.
Member receives newsletters.
user = list(gym_members.keys())[0]
#user = "Indra"
if user == "Joey" or user == "Antonio":
print("Found the user.")
Found the user.
user = "Indra"
if user == "Joey" or "":
print("Found the user.")
bool(-0.1)