Contents

  1. ascii.py
  2. ascii_table.py
  3. format_examples.py
  4. sales_tax2.py
  5. sales_tax.py
  6. temp_table_solution.py

ascii.py 1/6

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

[
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("The ASCII value is followed by the character.")

for i in range(33, 127):
    print(i, "-->", chr(i))
    

format_examples.py 3/6

[
top][prev][next]
# Formatting examples, from handout
# CSCI111

x = 10
y = 3.5
z = "apple"
print("{:6d}".format(x))
print("{:6.2f}".format(x))
print("{:6.2f}".format(y))
print("{:06.2f}".format(y))
print("{:+6.2f}".format(y))
print("{:^10s}".format(z))
print("*{:^11s}*".format(z))
print("{:5d} {:<7.3f}".format(x,y))

sales_tax2.py 4/6

[
top][prev][next]
# Compute the cost of an item, plus sales tax.
# The displayed cost uses a format specifier.
# by Sara Sprenkle

SALES_TAX=.053  # the sales tax in VA

value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

# version 0
print("\nVersion 0:")
print("Your item that cost ${:.2f} costs ${:.2f} with tax.".format(value, with_tax))

# version 1
print("\nVersion 1:")
print("Your item that cost ${:.2f}".format(value), end=' ')
print("costs ${:.2f} with tax.".format(with_tax))

# version 2
print("\nVersion 2:")
print("Your item that cost", "${:.2f}".format(value), end=' ')
print("costs", "${:.2f}".format(with_tax), "with tax.")

sales_tax.py 5/6

[
top][prev][next]
# Compute the cost of an item, plus sales tax
# Demonstrate need for/use of format specifiers
# by Sara Sprenkle

SALES_TAX=.053  # the sales tax in VA

# Test with a variety of values
value = eval(input("How much does your item cost? "))

with_tax = value * (1+SALES_TAX)

print("Your item that cost $", value, end=' ')
print("costs $", with_tax, "with tax.")

temp_table_solution.py 6/6

[
top][prev][next]
# Print out the table of temperatures
# By CS111

# Better to calculate these conversions but that's not the 
# focus today.

# First, figure out the format specifier for each column.
# - determine the type
# - determine the width
# - determine the flags and/or precision

# Second, fill in the values into each of those columns.

# Note how the column headers are based on the content of the tables

print("{:6s} {:>10s} {:>10s}".format("Temp F", "Temp C", "Temp K"))
print("{:6s} {:>10s} {:>10s}".format("-"*6, "-"*6, "-"*6))

ftemp = -459.67
ctemp = -273.15
ktemp=0

print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))


ftemp = 0
ctemp = -17.77778
ktemp= 255.222
print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))


ftemp = 32
ctemp = 0
ktemp= 273.15
print("{:6.1f} {:10.1f} {:10.1f}".format(ftemp, ctemp, ktemp))



Generated by GNU Enscript 1.6.6.