Contents
- ./consecutiveHeads.py
- ./consecutiveHeads_with_break.py
- ./demo_str.py
- ./game.py
- ./string_compare.py
- ./string_methods.py
- ./survey.py
./consecutiveHeads.py 1/7
[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")
# keep track of the total number of flips
total_flips = 0
# keep track of the number of consecutive heads
num_consecutive_heads = 0
while num_consecutive_heads < NUM_CONSECUTIVE:
# repeat flips
side = flipCoin()
# update the number of flips
total_flips += 1
# check what we flipped
if side == HEADS:
print("HEADS")
num_consecutive_heads += 1
else:
print("TAILS")
num_consecutive_heads = 0
print("The number of flips to", NUM_CONSECUTIVE, "is", total_flips)
./consecutiveHeads_with_break.py 2/7
[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")
# keep track of the total number of flips
total_flips = 0
# keep track of the number of consecutive heads
num_consecutive_heads = 0
while True:
# repeat flips
side = flipCoin()
# update the number of flips
total_flips += 1
# check what we flipped
if side == HEADS:
print("HEADS")
num_consecutive_heads += 1
else:
print("TAILS")
num_consecutive_heads = 0
if num_consecutive_heads == NUM_CONSECUTIVE:
break
print("The number of flips to", NUM_CONSECUTIVE, "is", total_flips)
./demo_str.py 3/7
[top][prev][next]
# Demonstrate strings and long strings
# by Sara Sprenkle
string = """This is a long string.
Like, really long.
Sooooo loooooong"""
print(string)
# Later: how could you do this with escape sequences?
./game.py 4/7
[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 5/7
[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_methods.py 6/7
[top][prev][next]
# Manipulate strings, using methods
# by Sara Sprenkle
sentence = input("Enter a sentence to mangle: ")
length = len(sentence)
# Question: What does the statement below do?
print("*", sentence.center(int(length*1.5)), "*")
upperSentence = sentence.upper()
print(upperSentence)
print(sentence)
print("Uppercase: ", sentence.upper())
print()
print("Lowercase: ", sentence.lower())
print()
# Answer before running...
print("Did sentence change?: ", sentence)
./survey.py 7/7
[top][prev][next]
# Demonstrate use of string concatenation, strings as constants
# by CS111
import sys
from random import *
SCALE_MIN=1
SCALE_MAX=100
SUBJECT = "Zendaya"
DIVIDER_LENGTH=50
NUM_TIMES=3
divider="-"*DIVIDER_LENGTH
prompt = "On a scale of " + str(SCALE_MIN) + " to " + str(SCALE_MAX) + ", how much do you like "+ SUBJECT
print(divider)
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)
else:
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.5.90.