Learn Python – NOTES 21 – 30

Program 21 – Evaluate for Leap Year

# if the year number isn’t evenly divisible by four, it’s a common year;
# otherwise, if the year number isn’t divisible by 100, it’s a leap year;
# otherwise, if the year number isn’t divisible by 400, it’s a common year;
# otherwise, it’s a leap year.

# output one of two possible messages, which are Leap year or Common year,
# depending on the value entered.

# verify if the entered year falls into the Gregorian era,
# and output a warning otherwise: Not within the Gregorian
# calendar period. Tip: use the != and % operators.

# == Evaluate of two values are equal
# % Modulus – Returns remainder ONLY – Discards value to left
# /= ?
# != Is not equal to
# // Truncates and disposes of any remainder
# ** Exponent

year = int(input(“Enter a year: “))
evaluation = “Not yet evaluated”

# Write your code here.
# Sample input: 2000 – – – Expected output: Leap year
# Sample input: 2015 – – – Expected output: Common year
# Sample input: 1999 – – – Expected output: Common year
# Sample input: 1996 – – – Expected output: Leap year
# Sample input: 1580 – – – Expected output: Not within the Gregorian calendar
# Robert Test Data – – – – 1111, 2222, 2224, 15555, 15556

if year <= 1580:
evaluation = “Not a Gregorian Date”
elif year%4 > 0:
evaluation = “This is a common year”
elif year%4 == 0:
evaluation = “This is a leap year”
elif year%2000 == 0:
evaluation = “This is a leap year”
elif year%100 == 0:
evaluation = “This is a leap year”
elif year%400 == 0:
evaluation = “This is a common year”
else:
evaluation = “ERROR DETECTED”

print (evaluation)

 

Program 22 – Send multiple values to multiple variables

x, y, z = 5, 10, 8

print(x > z) # False
print((y - 5) == x) # True

You can also do: 
x, y, z = 5, 10, 8
x, y, z = z, y, x

Program 23 – Else and Elif

If  True
   Executes
If  False
   Does not execute
If  True
   Executes

Elif will exit if True

Program 24 –  Endless While Loops

while True:
print(“I’m stuck inside a loop.”)
 
Exit with ^C
 

Program 25 – Equivalent expressions

Try to recall how Python interprets the truth of a condition, and note that these two forms are equivalent:
       while number != 0: and while number:.
The condition that checks if a number is odd can be coded in these equivalent forms, too:
             if number % 2 == 1: and if number % 2:.

Program 26 – Incrementing a Counter – Similar to C 

 counter -= 1   is the same as   counter = counter-1
 counter += 1   is the same as   counter = counter+1

Program 27 – Hurkle – A Game by Robert Andrews

NOTE:  This program works with valid entries.  It will return an error if you enter anything that is not between 1 and 99.

import random
secret_number = random.randint(1, 99)

import sys
# Multi Line Print

print(
“””
+============================+
| Welcome to Robert’s Hurkle.|
| Guess the Secret Number. |
+============================+
“””)
print (“The Secret Number is ” + str(secret_number))

MyGuess = 0
guesses = 0

while MyGuess != secret_number:
MyGuess = int(input(“Enter your secret number guess: “))
if MyGuess > secret_number: hint=”Lower”
else: hint= “Higher”

if MyGuess == secret_number:
print (“Congratulations. You guessed correctly.”)
print (“The number is ” + str(MyGuess))
print (“it took you ” + str(guesses+1) + ” guesses to get it right.”)
print()
elif MyGuess == 0:
print (“The number was ” + str(secret_number) + “.”)
sys.exit()   # You can also use break
else:
print (“Wrong. ” + str(MyGuess) + ” is not the number.”)
print (“Enter ZERO to give up.”)
guesses += 1
print (“So far, you guessed ” + str(guesses) + ” times.”)
print (“The secret number is ” + hint)
print()

 

Program 28 – Loops – If and For

  1. i = 0
  2. while i < 100:
  3.      # do_something()
  4.      i += 1

 

  1. for i in range(100):
  2.      # do_something()
  3. pass
  • for i in range(10):
  •      print(“The value of i is currently”, i)   
  •      # Note, space added automatically with comma

Starts with zero and ends with 9

You can also specify a range by two integers

for i in range(2, 8):

     print(“The value of i is currently”, i)

Program 29 -Hurkle – IMPROVED

import random
secret_number = random.randint(1, 99)

import sys

print(
“””
+============================+
| Welcome to Robert’s Hurkle.|
| Guess the Secret Number. |
+============================+
“””)
print (“The Secret Number is ” + str(secret_number))

MyGuess = 0
guesses = 0

while MyGuess != secret_number:
MyGuess = int(input(“Enter your secret number guess: “))
if MyGuess > secret_number: hint=”Lower”
else: hint= “Higher”

if MyGuess == secret_number:
print (“Congratulations. You guessed correctly.”)
print (“The number is ” + str(MyGuess))
print (“it took you ” + str(guesses+1) + ” guesses to get it right.”)
print()
elif MyGuess == 0:
print (“The number was ” + str(secret_number) + “.”)
sys.exit()
else:
print (“Wrong. ” + str(MyGuess) + ” is not the number.”)
print (“Enter ZERO to give up.”)
guesses += 1
print (“So far, you guessed ” + str(guesses) + ” times.”)
print (“The secret number is ” + hint)
print()

Program 30 – Time Delay and For Loops

import time
FindMe = “Ready or not. Here I come.”

# Write a for loop that counts to five.
i=1
while i<6:
print (i,”Mississippi”)
time.sleep(1)   # DELAYS PROGRAM 1 SECOND
# Body of the loop – print the loop iteration number and the word “Mississippi”.
i += 1
# Write a print function with the final message.
print (FindMe)