Contents
- binaryToDecimal.py
- mystery.py
- non_function_vars.py
- practice1.py
- practice2.py
- scope.py
- swap.py
binaryToDecimal.py 1/7
[top][prev][next]
# Convert binary numbers to decimal
# by CSCI 111
def main():
# get the binary number from the user, as a string
binNum = raw_input("Please enter a binary number: ")
isBinary = checkBinary(binNum)
while not isBinary : # equivalent to isBinary == False
print binNum, "is not a binary number."
binNum = raw_input("Please enter a binary number: ")
isBinary = checkBinary(binNum)
decVal = binaryToDecimal(binNum)
print binNum, "is", decVal
# Precondition: string (which is a string)
# Postcondition: return True iff string represents a binary number
# (meaning it contains only 0s and 1s); otherwise, returns False
def checkBinary( string ):
for char in string:
if not(char == "0" or char == "1"):
# above condition is equivalent to if char != "0" and char != "1":
return False
return True
# Precondition: binNum is a string that only contains 0s and 1s (binary string)
# Postcondition: returns binNum's value as a decimal value
def binaryToDecimal(binNum):
# initialize the decimal value of the number
decVal = 0
# go through the positions of the binary number
for pos in xrange(len( binNum )):
# compute the exponent
exp = len(binNum) - pos - 1
# convert the character at this position to an integer
bit = int(binNum[pos])
# compute the decimal value of this bit
val = bit * 2 ** exp
# add it to the decimal value
decVal += val
return decVal
main()
mystery.py 2/7
[top][prev][next]
# Mystery Program
# Used to demonstrate variable lifetimes and scope
def main():
x = 10
sum = sumEvens( x )
print "The sum of even #s up to", x, "is", sum
def sumEvens(limit):
total = 0
for x in xrange(0, limit, 2):
total += x
return total
main()
non_function_vars.py 3/7
[top][prev][next]
# Using variables that aren't part of any function
# by Sara Sprenkle
# create variables that aren't part of any function
non_func = 2
non_func_string = "aardvark"
def func():
print "In func: nf =", non_func
print "In func: nfs =", non_func_string
# Question: what happens when we try to assign the variables that
# aren't part of a function a value?
# Answer: program cannot execute!!!
#non_func = 6
#non_func_string = "dog"
func()
print non_func
print non_func_string
practice1.py 4/7
[top][prev][next]
# Exercising your knowledge of variable scope.
#
def main():
num = input("Enter a number to be squared: ")
squared = square(num)
print "The square is: ", squared
def square(n):
return n * n
main()
practice2.py 5/7
[top][prev][next]
# Exercising your knowledge of variable scope.
def main():
num = input("Enter a number to be squared: ")
squared = square(num)
print "The square is: ", squared
print "The original num was:", n
def square(n):
return n * n
main()
scope.py 6/7
[top][prev][next]
# scope.py
# Program illustrating scope
# Note: NOT good coding style
# by Sara Sprenkle
def main():
n = 30
e = 0.5
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+a+n
return c
main()
swap.py 7/7
[top][prev][next]
# Swap two variables
# UNSUCCESSFUL attempt at swapping two variables, x and y, in a function
# The values of x and y outside the function do NOT change
def swap(x, y):
tmp = x
x = y
y = tmp
x = 5
y = 7
swap(x, y)
# at the end, y should be 5 and x should be 7
print "x =", x
print "y =", y
Generated by GNU enscript 1.6.4.