Contents

  1. binaryToDecimal.py
  2. string_methods.py
  3. wheeloffortune.py

binaryToDecimal.py 1/3

[
top][prev][next]
# Convert binary numbers to decimal numbers
# CS111

import sys

print 
print "This program converts binary numbers to decimal numbers."
print

binary_string = raw_input("Enter a number in binary: ")

if not binary_string.isdigit() :
    print "That's not a binary string."
    sys.exit()

for bit in binary_string:
    if bit != "0" and bit != "1":
        print "That's not a binary string."
        sys.exit()

exponent = len(binary_string)-1

dec_value = 0

# for each bit in the binary string,
# multiply the bit by 2 to the appropriate power
# and add that to the decimal value, dec_value
for bit in binary_string:
    bit = int(bit)
    print bit,"* 2^%d" % exponent
    dec_value += bit * (2 ** exponent)

    exponent -= 1


print "The decimal value is", dec_value

string_methods.py 2/3

[
top][prev][next]
# Do stuff to strings, using methods
# by Sara Sprenkle

string = raw_input("Enter a sentence to mangle: ")

length = len(string)

print "*", string.center(int(length*1.5)), "*"

print string.upper()

print string.lower()

# Answer before running...
print "Did string change?: ", string


#print string.replace("a", "o")
#print "Did string change?: ", string

wheeloffortune.py 3/3

[
top][prev][next]
# Wheel of Fortune
#

PHRASE="RISKS OF EVOTING"
# PHRASE = "
PROMPT="Enter a letter or try to solve the puzzle: "
TITLE_WIDTH=50

# print out a nice header
print "*"*TITLE_WIDTH
print "WHEEL".center(TITLE_WIDTH)
print "OF".center(TITLE_WIDTH)
print "FORTUNE!".center(TITLE_WIDTH)
print "*"*TITLE_WIDTH 

# Display the current puzzle.  All the characters (except for spaces)
# are displayed as underscores
puzzle = ""
for char in PHRASE:
    if char != " ":
        puzzle += "_"
    else:
        puzzle += " "

print "The puzzle:", puzzle

print
print "Let's get started!"

# how many guesses it took to get it right
numGuesses = 1

guess = raw_input(PROMPT)

while guess.lower() != PHRASE.lower() :
    if len(guess) == 1:
        
        numOccurences = PHRASE.count(guess) + PHRASE.count(guess.swapcase())
        if numOccurences > 0:
            print "There are", numOccurences, guess + "'s", "in the phrase"
            # fill in puzzle
            updatedpuzzle=""
            for pos in xrange(len(PHRASE)):
                if PHRASE[pos] == guess or PHRASE[pos] == guess.swapcase():
                    updatedpuzzle += PHRASE[pos]
                else:
                    updatedpuzzle += puzzle[pos]
            puzzle = updatedpuzzle
            
            print "\nThe puzzle is", puzzle
        else:
            print "Sorry, there are no", guess + "'s", "in the word"
    else:
        print "You guessed incorrectly."
    
    print
    guess = raw_input(PROMPT)
    numGuesses += 1
        
    
print "Congratulations!  You solved the puzzle in", numGuesses, "guesses"

Generated by GNU enscript 1.6.4.