Contents
- break.py
- consecutiveHeads2.py
- consecutiveHeads.py
- file_read_while.py
- loop.py
- therapist.py
- while.py
- whilevsfor.py
break.py 1/8
[top][prev][next]
# Compares two versions of the same program, one uses break, one doesn't.
# Sara Sprenkle
# ---------- while LOOP ----------
# condition says when loop will continue
x=eval(input("Enter number: "))
while x % 2 != 0 :
print("Error!")
x = eval(input("Try again: "))
print(x, "is an even number. ")
# ---------- while LOOP USING break ----------
print("--"*10)
print("Again, but using break")
# have to look inside loop to know when it stops
while True :
x = eval(input("Enter number: "))
if x % 2 == 0 :
break
print("Error!")
print(x, "is an even number.")
consecutiveHeads2.py 2/8
[top][prev][next]
# Count how many times it takes to get 3 consecutive heads.
# This version uses a break statement
# By CSCI111
from random import randint
NUM_CONSECUTIVE = 3
print("This program finds how many coin flips are required before")
print("we get", NUM_CONSECUTIVE, "consecutive heads")
HEADS = 0
TAILS = 1
numConsHeads = 0
numFlips = 0
while True:
side = randint(0, 1)
if side == HEADS: # represents HEADS
numConsHeads += 1
print("HEADS")
else:
numConsHeads = 0
print("TAILS")
numFlips += 1
if numConsHeads == NUM_CONSECUTIVE:
break
print("It took", numFlips, "to reach", NUM_CONSECUTIVE, "heads")
consecutiveHeads.py 3/8
[top][prev][next]
# Count how many times it takes to get 3 consecutive heads
# By CSCI111
from random import randint
NUM_CONSECUTIVE = 3
print("This program finds how many coin flips are required before")
print("we get", NUM_CONSECUTIVE, "consecutive heads")
HEADS = 0
TAILS = 1
numConsHeads = 0
numFlips = 0
while numConsHeads < NUM_CONSECUTIVE :
side = randint(0, 1)
if side == HEADS: # represents HEADS
numConsHeads += 1
print("HEADS")
else:
numConsHeads = 0
print("TAILS")
numFlips += 1
print("It took", numFlips, "to reach", NUM_CONSECUTIVE, "heads")
file_read_while.py 4/8
[top][prev][next]
# Opens a file, reads the file one line at a time, and prints the
# contents
# by Sara Sprenkle
FILENAME="data/majors.dat"
# creates a new file object, opening the file in "read" mode
dataFile = open(FILENAME, "r")
# The following code reads in the file,
# line-by-line and displays the contents of the file
line = dataFile.readline()
# the empty string means that we've reached the end of the file.
while line != "":
# The check on the condition happens _before_ we strip off the
# ending whitespace. That ordering is important for correct file handling.
# Note that you don't want to use rstrip() if you want to preserve the
# whitespace at the end of the lines in the file.
line = line.rstrip()
print(line)
line = dataFile.readline()
# close the file with the method "close"
dataFile.close()
loop.py 5/8
[top][prev][next]
# What does this loop do?
# Sara Sprenkle
count = 1
while count > 0:
print(count)
count += 1
therapist.py 6/8
[top][prev][next]
# The Very Simple Therapist
# CSCI 111
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 = input("Tell me what's wrong.\n")
while user_input != "":
user_input = input("How does that make you feel?\n")
print("Thank you! Come again!")
while.py 7/8
[top][prev][next]
# Demonstrates a simple while loop.
# Trace through program and show what would be output.
# Sara Sprenkle
i = 0
while i < 10 :
print("i equals", i)
i+=1
print("Done", i)
whilevsfor.py 8/8
[top][prev][next]
# Code to compare 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 range(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.6.