Contents

  1. binaryToDecimal.py
  2. wheeloffortune.py

binaryToDecimal.py 1/2

[
top][prev][next]
# Converts a binary number into a decimal
# Modify to verify that the inputted number is valid.
# By CSCI111, 02.16.2011

import sys

# Read in the binary number as a string -- why?
num = raw_input("Enter the binary #: ")

# --------- Validate the user input ----------

# Make sure that the inputted number only contains numbers
# Equivalent to: if num.isdigit() == False:
while not num.isdigit():
    print num, "is not a valid number.  Please try again."
    num = raw_input("Enter the binary #: ")
    
# Make sure that the inputted number only contains 0s and 1s
for digit in num:
    if digit != "0" and digit != "1":
        print num, "is not a binary number."
        sys.exit(1)

# --------- Convert the binary number to a decimal number ----------

# accumulate the decimal value in this variable
decVal = 0

# go through the positions in the string
for pos in xrange(len(num)):
    # num[pos] is a string; need to convert to an int
    bit = int(num[pos])
    # calculate which "place" the current bit is at
    place = 2**(len(num)-pos-1)
    # add to the decimal value
    decVal += place * bit
    
print "The decimal value for", num, "is", decVal

wheeloffortune.py 2/2

[
top][prev][next]
# Wheel of Fortune
# By Sara Sprenkle

PHRASE="FOUR PUZZLES FROM CYBERSPACE"
# 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 alphabetical characters are displayed as underscores.
displayedPuzzle = ""
for char in PHRASE:
    if char.isalpha():
        displayedPuzzle += "_"
    else:
        displayedPuzzle += char

print "The puzzle:", displayedPuzzle

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 and guess.isalpha(): 
        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 += displayedPuzzle[pos]
            displayedPuzzle = updatedpuzzle
            
            print "\nThe puzzle is", displayedPuzzle
        else:
            print "Sorry, there are no", guess + "'s", "in the word"
    elif len(guess) != 1:
        # assumes that the user tried to solve the puzzle but got it wrong.
        print "\tSorry, that is not correct."
    else:
        print "\tError: You must guess a letter."
    
    print
    guess = raw_input(PROMPT)
    numGuesses += 1
        
print "Congratulations!  You solved the puzzle in", numGuesses, "guesses"



Generated by GNU enscript 1.6.4.