Contents
- binaryToDecimal.py
- oldmac.py
- scope.py
binaryToDecimal.py 1/3
[top][prev][next]
# Convert binary numbers to decimal numbers
# by Sara Sprenkle, 10.03.2007
# Modified to use functions: main and binaryToDecimal
def main():
print
print "This program converts binary numbers to decimal numbers."
print
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: ")
print "The decimal value is", binaryToDecimal(binary_string)
# Return True iff string str is a binary string
def isBinary(str):
isBinary = True
for ch in str:
if ch != "0" and ch != "1":
isBinary = False
# Wait until looked through each character in string
# to return answer
return isBinary
# Return True iff string str is a binary string
def isBinary_ReturnEarly(str):
if not str.isdigit():
return False
for ch in str:
if ch != "0" and ch != "1":
# As soon as one character isn't a 0 or 1,
# return False, so that don't do wasted work
return False
return True
# pre: binary_string is a string that contains only 0s and 1s
# post: returns the decimal value for the binary string
def binaryToInt( binary_string ):
# Alternatively, could do isBinary check here too,
# but, what would you return in that case??
exponent = len(binary_string)-1
dec_value = 0
for bit in binary_string:
bit = int(bit)
# Note: the printing was only for debugging;
# Now that it's in a function, shouldn't do that.
# print bit,"* 2^%d" % exponent
dec_value += bit * (2 ** exponent)
exponent -= 1
return dec_value
main()
oldmac.py 2/3
[top][prev][next]
# Print out verses of the song Old MacDonald
# by CS111, 10.15.2007
#
BEGIN_END = "Old McDonald had a farm"
EIEIO = ", E-I-E-I-O"
def main():
# call the verse function to print out a verse
verse("dog", "ruff")
verse("duck", "quack")
animal_type = "cow"
animal_sound = "moo"
verse(animal_type, animal_sound)
# QUESTION: What if called function as
# verse("ruff", "dog")
def verse(animal, sound):
print BEGIN_END + EIEIO
print "And on that farm he had a " + animal + EIEIO
print "With a " + sound + ", " + sound + " here"
print "And a "+ sound + ", " + sound + " there"
print "Here a " + sound
print "There a " + sound
print "Everywhere a " + sound + ", " + sound
print BEGIN_END + EIEIO
print
if __name__ == "__main__":
main()
scope.py 3/3
[top][prev][next]
# Examples illustrating scope
# Note: NOT good coding style
# by Sara Sprenkle, 10.15.2007
def main():
n = 30
e = 1
f = 2
g = 3
h = 4
print "\nBefore the call to function1,"
print "n = ", n
print "e = ", e
# QUESTION: How to change function1's call to execute other branch
i = function1(e, f, g, h)
print "\nAfter the call to function1,"
print "n = ", n
print "e = ", e
def function1(a, b, c, d):
# QUESTION: What would happen if the following line was commented
# out?
n = 400
print "\nIn function1, "
print "n = ", n
print "a = ", a
if a >= 1 :
a += b+n;
print "a = ", a, "after being modified"
return a
else :
c += d+m+n
return c
main()
Generated by GNU enscript 1.6.4.