Contents

  1. ascii.py
  2. ascii_table.py
  3. binaryToDecimal.py
  4. widthvar.py

ascii.py 1/4

[
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/4

[
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.py 3/4

[
top][prev][next]
# Converts a binary number into a decimal
# By CSCI111, 02.13.2012

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 #: ")

# accumulate the decimal value in this variable
decVal = 0

# go through the positions in the string
for pos in range(len(binnum)):
    # num[pos] is a string; need to convert to an int
    bit = int(binnum[pos])
    # figure out which "place" the current bit is at
    place = 2**(len(binnum)-pos-1)
    # add to the decimal value
    decVal += place * bit
    
print("The decimal value for", binnum, "is", decVal)

widthvar.py 4/4

[
top][prev][next]
# String format whose width is a variable
# 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.4.