Contents

  1. break.py
  2. consecutiveHeads2.py
  3. consecutiveHeads.py
  4. game.py
  5. loop.py
  6. therapist.py
  7. unconditional.py
  8. while.py
  9. whilevsfor.py

break.py 1/9

[
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/9

[
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
from game import *

NUM_CONSECUTIVE = 3

print("This program finds how many coin flips it took before")
print("we get", NUM_CONSECUTIVE, "consecutive heads")


consecutiveHeads.py 3/9

[
top][prev][next]
# Count how many times it takes to get 3 consecutive heads
# By CSCI111

from game import *

NUM_CONSECUTIVE = 3

print("This program finds how many coin flips it took before")
print("we get", NUM_CONSECUTIVE, "consecutive heads")

# need to keep track of consecutive heads and the number of flips
numConsecutiveHeads = 0
numFlipCoins = 0

# keep looping until we hit three consecutive heads
while numConsecutiveHeads < 3:
    headsOrTails = flipCoin()
    numFlipCoins += 1
    
    # if flip coin and it's HEADS, increment the count
    if headsOrTails == HEADS:
        numConsecutiveHeads += 1
        print("HEADS")
    else:
        # otherwise (it's TAILS), count goes back to 0
        numConsecutiveHeads = 0
        print("TAILS")
        
print("It took", numFlipCoins, "flips to get", NUM_CONSECUTIVE, "heads")

game.py 4/9

[
top][prev][next]
# Helpful functions for games 
# by CSCI111

from random import *

HEADS=0
TAILS=1

def flipCoin():
    """
    Simulates flipping a non-biased coin.
    returns either HEADS or TAILS.
    """
    return randint(HEADS, TAILS)
    
def testFlipCoin():
    """ tests the flipCoin function.
    Does not _guarantee_ success but helps gain confidence in correctness
    """
    numTests = 20
    numSuccesses = 0
    
    for x in range(numTests):
        flipped = flipCoin()
        
        if flipped == HEADS or flipped == TAILS:
            numSuccesses += 1
            
    print( numSuccesses, "out of", numTests, "tests passed.")
    
if __name__ == '__main__':
    testFlipCoin()

loop.py 5/9

[
top][prev][next]
# What does this loop do?
# By Sara Sprenkle

count = 1
while count > 0:
	#print(count)
	count += 1
print("After the loop")

therapist.py 6/9

[
top][prev][next]
# The Very Simple Therapist
# By 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!")

unconditional.py 7/9

[
top][prev][next]
# A very special Valentine's Day program
# BY CSCI 111

print("-"*60)
print("Happy [Belated] Valentine's Day!")
print("Press enter when you feel unconditional love.")
print("-"*60)

user_input = input("How was your day, valentine?\n")

while user_input != "":
    print("Remember: I will always love you.\n")
    user_input = input("Tell me more, valentine.\n")

print("I hope you feel loved.")

while.py 8/9

[
top][prev][next]
# Demonstrates a simple while loop.
# Trace through program and show what would be output.
# Sara Sprenkle

i = 0
while i < 5 :
    print("i equals", i)
    i+=1
print("Done", i)


whilevsfor.py 9/9

[
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.