Contents

  1. consecutiveHeads2.py
  2. consecutiveHeads.py
  3. game.py
  4. string_compare.py
  5. string_iteration.py
  6. survey.py

consecutiveHeads2.py 1/6

[
top][prev][next]
# Count how many times it takes to get 3 consecutive heads.
# This version uses a break statement
# 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 True:
    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")
    
    if numConsecutiveHeads == NUM_CONSECUTIVE:
        break
        
print("It took", numFlipCoins, "flips to get", NUM_CONSECUTIVE, "heads")

consecutiveHeads.py 2/6

[
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 3/6

[
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()

string_compare.py 4/6

[
top][prev][next]
# Program compares two strings
# by Sara Sprenkle

str1 = input("Enter a string to compare: ")
str2 = input("Compare '" + str1 + "' with what string? ")

print("-" * 40)

if str1 < str2 :
    print("Alphabetically,", str1, "comes before", str2 + ".")
elif str1 > str2:
    print("Alphabetically,", str2, "comes before", str1 + ".")
else:
    print("You tried to trick me!", str1, "and", str2, "are the same word!")

string_iteration.py 5/6

[
top][prev][next]
# Iterating through strings
# by Sara Sprenkle

phrase = input("Enter a phrase: ")

print("Iterate through phrase, using characters:")

for char in phrase:
    print(char)
    
print()

print("Iterate through phrase, using positions of characters:")
for pos in range(len(phrase)):
    print(pos, phrase[pos])

survey.py 6/6

[
top][prev][next]
# Demonstrate use of string concatenation, strings as constants
# by CS111

import sys
from random import *

SCALE_MIN=0
SCALE_MAX=10
SUBJECT = "Zendaya"
DIVIDER_LENGTH=50
NUM_TIMES=3

divider="-"*DIVIDER_LENGTH

print(divider)

prompt = "On a scale from " + str(SCALE_MIN) + " to " + str(SCALE_MAX) 
prompt = prompt + " how much do you like " + SUBJECT + "? "


for whichTime in range(NUM_TIMES):
    # ask survey question, respond with a wise crack
    rating = float(input(prompt))
    if rating < SCALE_MIN or rating > SCALE_MAX:
        print("Your rating is not in the valid range", SCALE_MIN, "to", SCALE_MAX)
        sys.exit(1)
    
    responseType = randint(0,3)
    if responseType <= 1:
        print(rating,"?!?  That's more than I do.")
    elif responseType == 2:
        print("yup, right on.")
    else:
        print("Nah,", rating, "is much too low.")
    
    print(divider)


Generated by GNU Enscript 1.6.6.