Contents

  1. ascii_dictionary.py
  2. break.py
  3. consecutiveHeads2.py
  4. consecutiveHeads.py
  5. using_dictionary.py

ascii_dictionary.py 1/5

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
# Sara Sprenkle

# create an empty dictionary
ascii= {}

ordValue = ord('a')

while ordValue <= ord('z'):
    # add mapping to dictionary of chr(ordValue) --> ordValue (ordinal value)
    char = chr(ordValue)
    ascii[char] = ordValue
    ordValue += 1

print(ascii)

break.py 2/5

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

[
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

consecutiveHeads = 0
numFlips = 0

while consecutiveHeads < NUM_CONSECUTIVE:
    if randint(0, 1) == HEADS:
        consecutiveHeads += 1
        print("HEADS")
    else:
        consecutiveHeads = 0
        print("TAILS")
    numFlips += 1
        
print("It took", numFlips, "to reach", NUM_CONSECUTIVE, "heads")

consecutiveHeads.py 4/5

[
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

consecutiveHeads = 0
numFlips = 0

while True:
    if randint(0, 1) == HEADS:
        consecutiveHeads += 1
        print("HEADS")
    else:
        consecutiveHeads = 0
        print("TAILS")
    numFlips += 1
    
    if consecutiveHeads == NUM_CONSECUTIVE: 
        break
        
print("It took", numFlips, "to reach", NUM_CONSECUTIVE, "heads")

using_dictionary.py 5/5

[
top][prev][next]
# Demonstrate use of dictionary, using ASCII values
#

# create an empty dictionary
ascii= {}

x = ord('a')

while x <= ord('z'):
    # add mapping to dictionary of chr(x) --> x (ordinal value)
    char = chr(x)
    ascii[char] = x
    x+=1

# iterates through the keys in the dictionary
for letter in ascii:
    # print the key and its associated value
    print(letter, ascii[letter])

# display the type that is returned by dictionary methods
print(type(ascii.keys()))
print(type(ascii.values()))
print("The number of keys is", len(ascii.keys()))

# iterate through the values
print("Iterate through the values:")
for val in ascii.values():
    print(val)
    
keyList = list(ascii.keys())
print("as <dict_keys>:\n", ascii.keys())
print("as a list:\n", keyList)

# printing in order by key
keysSorted = list(ascii.keys())
keysSorted.sort()

for letter in keysSorted:  # alternative: sorted(keysSorted)
    # print the key and its associated value
    print(letter, ascii[letter])

    
for letter in sorted(ascii):
    print(letter, ascii[letter])


Generated by GNU Enscript 1.6.6.