Contents
- ascii.py
- ascii_table.py
- binaryToDecimal_md.py
- binaryToDecimal_mg_brokendown.py
- binaryToDecimal_mg.py
- binaryToDecimal.py
- widthvar.py
ascii.py 1/7
[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/7
[top][prev][next]
# Create a table of numbers (ASCII) and their character equivalent
# by Sara Sprenkle
print("This program prints out the ASCII Table")
print("DEC CHAR")
print("-"*3, "-"*4)
for i in range(33, 127):
print("%3d %4s" % (i, chr(i)))
binaryToDecimal_md.py 3/7
[top][prev][next]
# Converts a binary number into a decimal
# This solutions moves from the right-most position
# to the left-most position of the binary number
# By CSCI111, Courtesy of Michael Dik
print("This program converts a binary number into a decimal number.")
# Read in the binary number as a string -- why?
binnum = input("Enter the binary #: ")
power = 0
decVal = 0
for x in range(1, len(binnum)+1):
bit = int(binnum[len(binnum)-x])
decVal += bit * 2 ** power
power = power + 1
print("The decimal value for", binnum, "is", decVal)
binaryToDecimal_mg_brokendown.py 4/7
[top][prev][next]
# Converts a binary number into a decimal.
# This solutions moves from the right-most position
# to the left-most position of the binary number.
# This version breaks down some of the steps from the
# original, to hopefully ease understanding.
#
# By CSCI111
print("This program converts a binary number into a decimal number.")
# Read in the binary number as a string -- why?
binnum = input("Input a binary number: ")
length = len(binnum)
decVal = 0
for x in range (0, length):
# binnum[x] is a string; we need to convert to a number
bit = eval(binnum[x])
exponent = length - x - 1
step = bit * 2**exponent
decVal = decVal + step
print("The decimal value for", binnum, "is", decVal)
binaryToDecimal_mg.py 5/7
[top][prev][next]
# Converts a binary number into a decimal.
# This solutions moves from the right-most position
# to the left-most position of the binary number
# By CSCI111 (courtesy of Max Gold)
print("This program converts a binary number into a decimal number.")
# Read in the binary number as a string -- why?
binnum = input("Input a binary number: ")
length = len(binnum)
answer = 0
for x in range (0, length):
step = eval(binnum[x]) * 2**(length - x - 1)
answer = answer + step
print("The decimal value for", binnum, "is", answer)
binaryToDecimal.py 6/7
[top][prev][next]
# Converts a binary number into a decimal.
# This version follows the algorithm given on the slides.
# This one iterates through the binary number from left to
# right, i.e., the power is decreasing.
# By CSCI111
print("This program converts a binary number into a decimal number.")
# Read in the binary number as a string -- why?
binnum = input("Enter the binary number: ")
#binnum="10"
exponent = len(binnum) - 1
decVal = 0
for bitAsChar in binnum:
bitAsInt = int(bitAsChar)
decVal += bitAsInt * 2 ** exponent
exponent -= 1
print("The decimal value for", binnum, "is", decVal)
widthvar.py 7/7
[top][prev][next]
# String format whose width is a variable.
# An bonus program, for those interested in creating
# dynamically-sized format specifiers.
# by Sara Sprenkle
word = input("Enter a word: ")
width = len(word)
# create the format specifier string with the length of the word
formatspec = "%" + str(width) + "i"
print()
print("Right justify based on length of the word")
print()
print(word)
print(width*"-")
for x in range(0, 101, 10):
print( formatspec % x )
Generated by GNU Enscript 1.6.6.