Contents
- ./binaryToDecimalIterateOverCharacters.py
- ./binaryToDecimalIterateOverExponents.py
- ./binaryToDecimal.py
- ./demo_str.py
- ./escape_sequences.py
./binaryToDecimalIterateOverCharacters.py 1/5
[top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the characters in the string,
# i.e., from left to right in the string.
# 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():
test.testEqual(binaryToDecimal("0"), 0)
test.testEqual(binaryToDecimal("1"), 1)
test.testEqual(binaryToDecimal("1000"), 8)
test.testEqual(binaryToDecimal("1111"), 15)
test.testEqual(binaryToDecimal("10110"), 22)
def binaryToDecimal(binary):
"""
Pre: binary should be a string containing only 0s and 1s
Post: converts binary into a decimal value and returns that value
"""
decVal = 0
exponent = len(binary)
for char in binary:
bit = int(char)
exponent -= 1
decVal += bit * 2**exponent
return decVal
#testBinaryToDecimal()
main()
./binaryToDecimalIterateOverExponents.py 2/5
[top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents,
# i.e., from right to left over the binary number.
# 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():
test.testEqual(binaryToDecimal("0"), 0)
test.testEqual(binaryToDecimal("1"), 1)
test.testEqual(binaryToDecimal("1000"), 8)
test.testEqual(binaryToDecimal("1111"), 15)
test.testEqual(binaryToDecimal("10110"), 22)
def binaryToDecimal(binary):
"""
Pre: binary should be a string containing only 0s and 1s
Post: converts binary into a decimal value and returns that value
"""
decVal = 0
for exponent in range(len(binary)):
index = len(binary) - (exponent + 1)
char = binary[index]
if char == "1":
decVal += 2**exponent
# Alternative:
# bit = int(char)
# decVal += bit * 2 ** exponent
return decVal
testBinaryToDecimal()
#main()
./binaryToDecimal.py 3/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.")
binNum = input("Enter a binary number: ")
print(binNum, "has a decimal value of", binaryToDecimal(binNum))
def testBinaryToDecimal():
"""
tests the binaryToDecimal function
"""
test.testEqual( binaryToDecimal("0"), 0 )
test.testEqual( binaryToDecimal("1"), 1 )
test.testEqual( binaryToDecimal("10"), 2 )
test.testEqual( binaryToDecimal("11"), 3 )
test.testEqual( binaryToDecimal("100"), 4)
test.testEqual( binaryToDecimal("1101"), 13)
test.testEqual( binaryToDecimal("10110"), 22)
test.testEqual( binaryToDecimal("0000"), 0 )
def binaryToDecimal(binaryNum):
"""
Converts binary number into a decimal number
Pre: binaryNum should be a string containing only 0s and 1s
Post: convert binaryNum into a decimal value and return that value
as an integer
"""
testBinaryToDecimal()
#main()
./demo_str.py 4/5
[top][prev][next]
# Demonstrate long strings and escape sequences
# by Sara Sprenkle
string = """This is a long string.
Like, really long.
Sooooo loooooong"""
print(string)
print("\n") # how many blank lines does this print?
print("To print a \\, you must use \"\\\\\"")
./escape_sequences.py 5/5
[top][prev][next]
# Practice with escape sequences
# By CS111
# Display To print a tab, you must use '\t'.
print("To print a tab, you must use \'\\t\'.")
print("To print a tab, you must use '\\t'.")
print('To print a tab, you must use \'\\t\'.')
# how many blank lines does this print?
print("\n")
# Display I said, "How are you?"
print("I said, \"How are you?\"")
print('I said, \"How are you?\"')
print('I said, "How are you?"')
Generated by GNU Enscript 1.6.5.90.