Contents

  1. birthyear.py
  2. eval_cond.py
  3. inf_loop.py
  4. num2lettergrade.py

birthyear.py 1/4

[
top][prev][next]
# Example of if/elif/else 
# Calculates a person's birthyear, given the current year and their age.
# by CS111, 09/28/07

print "This program determines your birth year"
print "given your age and current year"
print
age = input("Enter your age >> ")

if age > 110:
    print "Don't be ridiculous, you can't be that old."
elif age < 0 :
    print "You're a negative age?  I don't think so."
else:
    currentYear = input("Enter the current year >> ")
    if currentYear < 0 :
        print "We're only dealing with AD"
    else : 
        birthyear = currentYear - age
        print
        print "You were either born in", birthyear, "or", birthyear-1

eval_cond.py 2/4

[
top][prev][next]
# Practice evaluating conditions
# by Sara Sprenkle for CS111, 09.28.2007

x = 2
y = 3
z = 4

b = x==2
c = not b
d = (y<4) and (z<3)
d = (y<4) or (z<3)
d = not d

print b, c, d

inf_loop.py 3/4

[
top][prev][next]
# Demonstrates an infinite loop.
# Use Control-C to kill in Linux
# by Sara Sprenkle for CS111, 09.28.2007

count = 1
while count > 0:
  print count
  count += 1


num2lettergrade.py 4/4

[
top][prev][next]
# Determine the letter grade from a number grade
# by CS111, 09.28.2007

num_grade = input("Enter the numeric grade: ")

# Enforce that the grade must be between 0 and 100, inclusive
if num_grade < 0 or num_grade > 100:
    print "The numeric grade must be between 0 and 100"
else :
    if num_grade >= 90 :
        letter_grade = "A"
    elif num_grade >= 80 :
        letter_grade = "B"
    elif num_grade >= 70 :
        letter_grade = "C"
    elif num_grade >= 60 :
        letter_grade = "D"
    else:
        letter_grade = "F"

    print num_grade, "is a", letter_grade

# INSTEAD: Could have used an and
# if num_grade >= 0 and num_grade <= 100:
#     do computation
# else:
#     print error message

Generated by GNU enscript 1.6.4.