# This is a one-line comment
'''This is how you can do
several commented lines
without having to type # over and over'''
'''Simplest example: if the amount you want to withdraw
is greater than your balance, then stop the transaction.
Note that you need a colon after the if or else. Also,
it's fine to have multiple lines as long as they are indented.
Also, you don't need an else clause if you don't want one'''
balance = 500
withdrawal = 200 # now change to 1200 and see what happens
if(balance < withdrawal):
print("I'm sorry, but you cannot withdraw that amount.")
else:
balance = balance - withdrawal
print("Sure, no problem. Here is your money. New balance is",balance)
# An example with no else-clause
if(balance >= withdrawal):
balance = balance - withdrawal
print("Here is your money. New balance is",balance)
# Chained Conditionals use if,elif,else
# The interpreter will only evaluate one of these blocks of code
x = 0
if(x > 0):
print('x is positive')
elif(x < 0):
print('x is negative')
else:
print('x is zero')
# This example is for the students to code
'''Note: the input function is how you get an input from the user
eval() is how the machine knows you want to treat this input as an int'''
grade = eval(input("Please enter your grade (between 0 and 100): "))
if(grade > 89):
lt_grade = 'A'
print('Great job!')
elif(grade > 79):
lt_grade = 'B'
elif(grade > 69):
lt_grade = 'C'
elif(grade > 59):
lt_grade = 'D'
else:
lt_grade = 'F'
print('Please come see me')
print("Letter equivalent of",grade,"is",lt_grade)
# If the grade is 90 or above then none of the elif's are entered.
# If it's 80-89 then the condition for the if-statement is false
# but the condition for the first elif is satisfied
# Nested conditionals example
balance = 500
withdrawal = 200
if(withdrawal > balance):
print('Sorry, but you do not have that much money.')
else:
if(withdrawal == balance):
balance = 0
print('Okay, here is all your money')
else:
balance = balance - withdrawal
print('Here is your money. New balance is',balance)
# Simple example involving booleans
x = 2
y = -7
if(x > 0 and y > 0):
print("Both are positive")
elif(x < 0 and y < 0):
print("Both are negative")
else:
print("At least one is non-positive and the other non-negative")
# More complicated example
'''In the Gregorian calendar, for any year after 1582:
If the year is divisible by 4 then it's a leap year unless it's
divisible by 100. The exception is years divisible by 400, which are leap years
So 2004 is a leap year, 2100 is not, but 2000 and 2400 are.'''
# We'll also use the "mod" function %. So x % y is read "x mod y" and
# means the remainder when x is divided by y.
# So 2004 % 4 is 0 because 4 divides 2004 evenly, with no remainder.
year = eval(input("Please enter a year after 1582: "))
if(year % 4 == 0):
if(year % 100 == 0):
if(year % 400 == 0):
answer = 'Yes'
# These are years like 2000 or 2400
else:
answer = 'No'
# These are years like 2100
# This else refers to year mod 100, so we get here if not divisible by 100
else:
answer = 'Yes'
# These are years like 2004
else:
answer = 'No'
# These are years like 1997
print(answer)
# A way using Booleans and avoiding nested conditionals
# Divisible by 4 but not by 100
if(year % 4 == 0 and year % 100 != 0):
answer = 'Yes'
# Divisible by 100 but not by 400
elif(year % 100 == 0 and year % 400 != 0):
answer = 'No'
# Divisible by 400 means automatically a leap year
elif(year % 400 == 0):
answer = 'Yes'
# Not divisible by 4 or 100 or 400
else:
answer = 'No'
print(answer)
''' Is there a faster way to do this? '''
if(year % 4 != 0):
print('No')
elif(year % 100 == 0 and year % 400 != 0):
print('No')
else:
print('Yes')
# Thinking through the problem carefully beforehand can save a lot of time
''' Is there an even faster way?'''
# As soon as one of the if/elif/else is satisfied it stops checking.
# It'll never ask if year is divisible by 100 unless it's already
# checked that year is not divisible by 400.
if(year % 400 == 0):
answer = 'Yes'
elif(year % 100 == 0):
answer = 'No'
elif(year % 4 == 0):
answer = 'Yes'
else:
answer = 'No'
print(answer)