Contents
- binaryToDecimal.py
- flow_example.py
- lab2.4.wfunctions.py
binaryToDecimal.py 1/3
[top][prev][next]
# Convert binary numbers to decimal
# by CSCI 111
import sys
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
# get the binary number from the user, as a string
binNum = raw_input("Please enter a binary number: ")
decVal = binaryToDecimal(binNum)
print binNum, "is", decVal
flow_example.py 2/3
[top][prev][next]
def max(num1, num2):
result = 0
if num1 >= num2:
result = num1
else:
result = num2
return result
x = 2
y = input("Enter a number: ")
z = max(x, y)
print "The max is", z
lab2.4.wfunctions.py 3/3
[top][prev][next]
# lab2.4.py
# Displays equivalents for Olympic running distances in
# meters, kilometers, yards, miles
#
# UPDATE 3/1/2010: added a function to convert meters to miles
# conversion constants
M2KILO=.001
M2YARD=1.094
M2MILE=.0006215
def metersToMiles(meters):
miles = meters * M2MILE
return miles
# distances in meters
dist1=100
dist2=200
dist3=400
dist4=800
# print column headings
print "Meters Kilometers Yards Miles"
print "-"*32
# calculate conversions, print data for conversion table
kilo1=dist1*M2KILO
yard1=dist1*M2YARD
mile1=metersToMiles(dist1)
print "%6.0i %11.3f %6.1f %6.3f" % (dist1, kilo1, yard1, mile1)
kilo2=dist2*M2KILO
yard2=dist2*M2YARD
mile2=metersToMiles(dist2)
print "%6.0i %11.3f %6.1f %6.3f" % (dist2, kilo2, yard2, mile2)
kilo3=dist3*M2KILO
yard3=dist3*M2YARD
mile3=metersToMiles(dist3)
print "%6.0i %11.3f %6.1f %6.3f" % (dist3, kilo3, yard3, mile3)
kilo4=dist4*M2KILO
yard4=dist4*M2YARD
mile4=metersToMiles(dist4)
print "%6.0i %11.3f %6.1f %6.3f" % (dist4, kilo4, yard4, mile4)
Generated by GNU enscript 1.6.4.