Contents
- ascii.py
- ascii_table.py
- binaryToDecimal.py
- binaryToDecimal_starter.py
- test.py
ascii.py 1/5
[top][prev][next]
# Conversion of a text message into ASCII
# by Sara Sprenkle
print()
print("This program converts a textual message into a sequence")
print("of numbers representing the ASCII encoding of the message.")
print()
message = input("Enter the message to encode: ")
print()
print("Here are the ASCII codes for '" + message + "':")
for ch in message:
print(ord(ch), end=" ")
print()
ascii_table.py 2/5
[top][prev][next]
# Create a table of numbers (ASCII) and their character equivalent
# by Sara Sprenkle
print("This program prints out part of the ASCII Table")
print("DEC CHAR")
print("-"*3, "-"*4)
for i in range(33, 127):
print("{:3d} {:4s}".format(i, chr(i)))
binaryToDecimal.py 3/5
[top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents (i.e., from right to left).
# By CSCI111
import test
def main():
print("This program converts a binary number into a decimal number.")
binaryNum = input("What is your binary number? ")
decVal = binaryToDecimal(binaryNum)
print("The decimal value of", binaryNum, "is", decVal)
def testBinaryToDecimal():
"""
tests the binaryToDecimal function
"""
test.testEqual( binaryToDecimal("0"), 0 )
test.testEqual( binaryToDecimal("1"), 1 )
test.testEqual( binaryToDecimal("10"), 2 )
test.testEqual( binaryToDecimal("1000"), 8 )
test.testEqual( binaryToDecimal("1101"), 13 )
test.testEqual( binaryToDecimal("1111"), 15 )
test.testEqual( binaryToDecimal("10110"), 22 )
def binaryToDecimal(binaryStr):
"""
Given the binary number as a string of 0s and 1s,
return the binary number as a decimal.
"""
decValue = 0
exponent = 0
for pos in range(len(binaryStr)-1, -1, -1):
bit = int(binaryStr[pos])
value = bit * 2 ** exponent
decValue += value
exponent += 1
return decValue
testBinaryToDecimal()
binaryToDecimal_starter.py 4/5
[top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents.
# Exercise: implement two ways:
# --> iterate over the binary string from the last character to the first
# --> iterate over the binary string from the first character to the last
# By CSCI111
import test
def main():
print("This program converts a binary number into a decimal number.")
def testBinaryToDecimal():
"""
tests the binaryToDecimal function
"""
test.testEqual( binaryToDecimal(), )
def binaryToDecimal(binary):
"""
"""
testBinaryToDecimal()
test.py 5/5
[top][prev][next]
# From How to Think Like a Computer Scientist textbook
def testEqual(actual, expected):
if type(expected) == type(1):
# they're integers, so check if exactly the same
if actual == expected:
print('Pass')
return True
elif type(expected) == type(1.11):
# a float is expected, so just check if it's very close, to allow for
# rounding errors
if abs(actual-expected) < 0.00001:
print('Pass')
return True
else:
# check if they are equal
if actual == expected:
print('Pass')
return True
print('Test Failed: expected ' + str(expected) + ' but got ' + str(actual))
return False
Generated by GNU Enscript 1.6.6.