Contents

  1. eval_cond.py
  2. fines.elif.py
  3. infiniteloop.py
  4. while.py
  5. whilevsfor.py

eval_cond.py 1/5

[
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

fines.elif.py 2/5

[
top][prev][next]
# Calculate fines
# Converted to if-elif-else structure
# What are pros/cons of this version compared to other version
# CS111

clockspeed = input("Enter the clocked speed: ")
speedlimit = input("Enter the speed limit: ")

if clockspeed <= speedlimit:
    print "Continue safe driving practices"
elif clockspeed > 90:
    diff = clockspeed - speedlimit
    fine = 250 + 5 * diff
    print "Slow down!  You've been fined $" + str(fine) + "."
else:         
    diff = clockspeed - speedlimit
    fine = 50 + 5 * diff
    print "Slow down!  You've been fined $" + str(fine) + "."

infiniteloop.py 3/5

[
top][prev][next]
# Infinite Loop

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

while.py 4/5

[
top][prev][next]
# Example of a while loop
# by Sara Sprenkle

# WHILE LOOP

print "While Loop Demo"
i=0
while i < 10:
    print "i equals", i
    i += 1
print "Done", i


whilevsfor.py 5/5

[
top][prev][next]
# Compares a while loop with a for loop
# by Sara Sprenkle

# WHILE LOOP

print "While Loop Demo"
i=0
while i < 10:
    print "i equals", i
    i += 1
print "Done", i

# FOR LOOP

print "\nFor Loop Demo"
for i in xrange(10):
    print "i equals", i

print "Done", i

Generated by GNU enscript 1.6.4.