Contents
- ./consecutiveHeads.py
- ./consecutiveHeads_with_break.py
- ./demo_str.py
- ./game.py
- ./string_methods.py
- ./survey.py
./consecutiveHeads.py 1/6
[top][prev][next]
# Count how many times it takes to get 3 consecutive heads
# By CSCI111
from game import *
GOAL_CONSECUTIVE = 3
print("This program finds how many coin flips it took before")
print("we get", GOAL_CONSECUTIVE, "consecutive heads")
num_cons_heads = 0
num_flips = 0
while num_cons_heads < GOAL_CONSECUTIVE:
flipped = flip_coin()
num_flips += 1
print(num_flips, ":", sep="", end=" ")
if flipped == HEADS:
num_cons_heads += 1
print("HEADS!")
else:
num_cons_heads = 0
print("TAILS!")
print("It took", num_flips, "flips to get to", GOAL_CONSECUTIVE, "heads")
./consecutiveHeads_with_break.py 2/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 *
GOAL_CONSECUTIVE = 3
print("This program finds how many coin flips it took before")
print("we get", NUM_CONSECUTIVE, "consecutive heads")
num_cons_heads = 0
num_flips = 0
while True:
flipped = flip_coin()
num_flips += 1
print(num_flips, ":", sep="", end=" ")
if flipped == HEADS:
num_cons_heads += 1
print("HEADS!")
else:
num_cons_heads = 0
print("TAILS!")
if num_cons_heads == NUM_CONSECUTIVE:
break
print("It took", num_flips, "flips to get to", NUM_CONSECUTIVE, "heads")
./demo_str.py 3/6
[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 create the same string using escape sequences?
./game.py 4/6
[top][prev][next]
# Helpful functions for games
# Only partial implementation for today's problems
# by CSCI111
from random import *
HEADS=0
TAILS=1
def flip_coin():
"""
Simulates flipping a non-biased coin.
returns either HEADS or TAILS.
"""
return randint(HEADS, TAILS)
def testFlipCoin():
""" tests the flip_coin function.
Does not _guarantee_ success but helps gain confidence in correctness
"""
numTests = 20
numSuccesses = 0
for x in range(numTests):
flipped = flip_coin()
if flipped == HEADS or flipped == TAILS:
numSuccesses += 1
print( numSuccesses, "out of", numTests, "tests passed.")
if __name__ == '__main__':
testFlipCoin()
./string_methods.py 5/6
[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 6/6
[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
## TODO
# create the prompt using the above constants such that the prompt is
# "On a scale of 1 to 100, how much do you like Zendaya?"
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.