Contents
- break.py
- loop.py
- sumtillnothing.py
- therapist.py
- while.py
- whilevsfor.py
break.py 1/6
[top][prev][next]
# Demonstrates use of break statement, but this is a pretty dumb program.
# ONLY use break statements with while loops
# Sara Sprenkle
x=10
i = 0
count = x
while i < 10 :
if count < 100 :
i += 1
else:
break
print "Done", i
loop.py 2/6
[top][prev][next]
# What does this loop do?
# Sara Sprenkle
count = 1
while count > 0:
print count
count += 1
sumtillnothing.py 3/6
[top][prev][next]
# Sum till user enters nothing.
# CSCI 111, 02.02.2011
print "Sums up numbers until you enter nothing."
user_input = raw_input("Enter your first number: ")
sum = 0
while user_input != "":
sum += float(user_input)
user_input = raw_input("Enter the next number (nothing to stop): ")
print "The sum is", sum
therapist.py 4/6
[top][prev][next]
# The Simple Therapist
# CSCI 111, 02.02.2011
print "-"*60
print "Welcome to computerized therapy!"
print "You will get your money's worth."
print "Our session is over when you have nothing more to tell me."
print "-"*60
user_input = raw_input("Tell me what's wrong.\n")
while user_input != "":
user_input = raw_input("How does that make you feel?\n")
print "Thank you! Come again!"
while.py 5/6
[top][prev][next]
# Demonstrates a simple while loop
# Sara Sprenkle
i = 0
while i < 10 :
print "i equals", i
i+=1
print "Done", i
whilevsfor.py 6/6
[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
# To give exactly the same output as the while loop, would need to print out i+1
Generated by GNU enscript 1.6.4.