Contents

  1. binaryToDecimal.html
  2. binaryToDecimal.py
  3. menu.py
  4. menu_withfunctions.py
  5. menu_withoutfunc.py
  6. style.css
  7. swap.py

binaryToDecimal.html 1/7

[
top][prev][next]
<div class="highlight"><pre><span class="c"># Convert binary numbers to decimal numbers</span>
<span class="c"># by Sara Sprenkle, 10.03.2007</span>

<span class="k">print</span> 
<span class="k">print</span> <span class="s">&quot;This program converts binary numbers to decimal numbers.&quot;</span>
<span class="k">print</span>

<span class="k">def</span> <span class="nf">isBinary</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">str</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
        <span class="k">return</span> <span class="bp">False</span>
    
    <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">ch</span> <span class="o">!=</span> <span class="s">&quot;0&quot;</span> <span class="ow">and</span> <span class="n">ch</span> <span class="o">!=</span> <span class="s">&quot;1&quot;</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="bp">True</span>

<span class="n">binary_string</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">&quot;Enter a number in binary: &quot;</span><span class="p">)</span>

<span class="k">while</span> <span class="ow">not</span> <span class="n">isBinary</span><span class="p">(</span><span class="n">binary_string</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&quot;Sorry, that is not a binary string&quot;</span>
    <span class="n">binary_string</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">&quot;Enter a number in binary: &quot;</span><span class="p">)</span>

<span class="c"># only get to here if the string is binary</span>

<span class="n">exponent</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">binary_string</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span>
    
<span class="n">dec_value</span> <span class="o">=</span> <span class="mi">0</span>

<span class="k">for</span> <span class="n">bit</span> <span class="ow">in</span> <span class="n">binary_string</span><span class="p">:</span>
    <span class="n">bit</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">bit</span><span class="p">)</span>
    <span class="k">print</span> <span class="n">bit</span><span class="p">,</span><span class="s">&quot;* 2^</span><span class="si">%d</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">exponent</span>
    <span class="n">dec_value</span> <span class="o">+=</span> <span class="n">bit</span> <span class="o">*</span> <span class="p">(</span><span class="mi">2</span> <span class="o">**</span> <span class="n">exponent</span><span class="p">)</span>
    
    <span class="n">exponent</span> <span class="o">-=</span> <span class="mi">1</span>
    
    
<span class="k">print</span> <span class="s">&quot;The decimal value is&quot;</span><span class="p">,</span> <span class="n">dec_value</span>
</pre></div>

binaryToDecimal.py 2/7

[
top][prev][next]
# Convert binary numbers to decimal numbers
# by Sara Sprenkle, 10.03.2007

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

def isBinary(str):
    if not str.isdigit():
        return False
    
    for ch in str:
        if ch != "0" and ch != "1":
            return False
    return True

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

while not isBinary(binary_string):
    print "Sorry, that is not a binary string"
    binary_string = raw_input("Enter a number in binary: ")

# only get to here if the string is binary

exponent = len(binary_string)-1
    
dec_value = 0

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


menu.py 3/7

[
top][prev][next]
def printWelcomeScreen(name):
    welcome = "Welcome to " + name + "!"
    length = len(welcome)
    print length*"-"

def printMenu():
    print "You have some options for what to do: "
    print "Enter an 'F' to find a song"
    print "Enter an 'S' to sort by Song title"
    print "Enter an 'A' to sort by Album"
    print "Enter an 'R' to sort by aRtist name"
    print "Enter an 'H' to list your options again"
    print "Enter a 'Q' to quit"

menu_withfunctions.py 4/7

[
top][prev][next]
# Using functions from menu module
# by Sara Sprenkle

import menu

STOP_OPTION = 'Q'

menu.printWelcomeScreen("MusicManager")
menu.printMenu()

menuChoice = raw_input("Which option do you choose? ")
menuChoice = menuChoice.lower()

while menuChoice != STOP_OPTION :
    menu.printMenu()
    menuChoice = raw_input("Which option do you choose? ")
    menuChoice = menuChoice.lower()

menu_withoutfunc.py 5/7

[
top][prev][next]
# What code would look like without functions
# by Sara Sprenkle

STOP_OPTION = 'Q'

name = "Music Program"

welcome = "Welcome to " + name + "!"
length = len(welcome)
print length*"-"

print "You have some options for what to do: "
print "Enter an 'F' to find a song"
print "Enter an 'S' to sort by Song title"
print "Enter an 'A' to sort by Album"
print "Enter an 'R' to sort by aRtist name"
print "Enter an 'H' to list your options again"
print "Enter a 'Q' to quit"
    
menuChoice = raw_input("Which option do you choose? ")

while menuChoice != STOP_OPTION :
    print "You have some options for what to do: "
    print "Enter an 'F' to find a song"
    print "Enter an 'S' to sort by Song title"
    print "Enter an 'A' to sort by Album"
    print "Enter an 'R' to sort by aRtist name"
    print "Enter an 'H' to list your options again"
    print "Enter a 'Q' to quit"
    menuChoice = raw_input("Which option do you choose? ")

style.css 6/7

[
top][prev][next]
.c { color: #408080; font-style: italic } /* Comment */
.err { border: 1px solid #FF0000 } /* Error */
.k { color: #008000; font-weight: bold } /* Keyword */
.o { color: #666666 } /* Operator */
.cm { color: #408080; font-style: italic } /* Comment.Multiline */
.cp { color: #BC7A00 } /* Comment.Preproc */
.c1 { color: #408080; font-style: italic } /* Comment.Single */
.cs { color: #408080; font-style: italic } /* Comment.Special */
.gd { color: #A00000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #000080; font-weight: bold } /* Generic.Heading */
.gi { color: #00A000 } /* Generic.Inserted */
.go { color: #808080 } /* Generic.Output */
.gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.gt { color: #0040D0 } /* Generic.Traceback */
.kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.kp { color: #008000 } /* Keyword.Pseudo */
.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.kt { color: #008000; font-weight: bold } /* Keyword.Type */
.m { color: #666666 } /* Literal.Number */
.s { color: #BA2121 } /* Literal.String */
.na { color: #7D9029 } /* Name.Attribute */
.nb { color: #008000 } /* Name.Builtin */
.nc { color: #0000FF; font-weight: bold } /* Name.Class */
.no { color: #880000 } /* Name.Constant */
.nd { color: #AA22FF } /* Name.Decorator */
.ni { color: #999999; font-weight: bold } /* Name.Entity */
.ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.nf { color: #0000FF } /* Name.Function */
.nl { color: #A0A000 } /* Name.Label */
.nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.nt { color: #008000; font-weight: bold } /* Name.Tag */
.nv { color: #19177C } /* Name.Variable */
.ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mf { color: #666666 } /* Literal.Number.Float */
.mh { color: #666666 } /* Literal.Number.Hex */
.mi { color: #666666 } /* Literal.Number.Integer */
.mo { color: #666666 } /* Literal.Number.Oct */
.sb { color: #BA2121 } /* Literal.String.Backtick */
.sc { color: #BA2121 } /* Literal.String.Char */
.sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.s2 { color: #BA2121 } /* Literal.String.Double */
.se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.sh { color: #BA2121 } /* Literal.String.Heredoc */
.si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.sx { color: #008000 } /* Literal.String.Other */
.sr { color: #BB6688 } /* Literal.String.Regex */
.s1 { color: #BA2121 } /* Literal.String.Single */
.ss { color: #19177C } /* Literal.String.Symbol */
.bp { color: #008000 } /* Name.Builtin.Pseudo */
.vc { color: #19177C } /* Name.Variable.Class */
.vg { color: #19177C } /* Name.Variable.Global */
.vi { color: #19177C } /* Name.Variable.Instance */
.il { color: #666666 } /* Literal.Number.Integer.Long */

swap.py 7/7

[
top][prev][next]
# Showing pass-by-value of functions with swap function
# Sara Sprenkle, 10.

# Attempt to swap two values
def swap( x, y ):
    temp = x
    x = y
    y = temp


x = 5
y = 10

print "x is", x
print "y is", y

swap(x,y)

print "x is", x
print "y is", y

Generated by GNU enscript 1.6.4.