Contents

  1. format_examples.py
  2. sales_tax2.py
  3. sales_tax.py
  4. temp_table.py

format_examples.py 1/4

[
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("{:^11s}".format(z))


print("*{:^11s}*".format(z))
print("{:5d} {:<7.3f}".format(x,y))

sales_tax2.py 2/4

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

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

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

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

# Suggested process: 
# 1. figure out the format specifier for each column.
#    - determine the type
#    - determine the width
#    - determine the flags and/or precision
# 2. fill in the values into each of those columns.

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

# display the headers
print("{:>12s}{:>12s}{:>12s}".format("Temp F", "Temp C", "Temp K"))
print("{:>12s}{:>12s}{:>12s}".format("-"*6, "-"*6, "-"*6))

# display the data
ftemp = -459.67
ctemp = -273.15
ktemp=0

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

ftemp = 0
ctemp = -17.77778
ktemp= 255.222

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

ftemp = 32
ctemp = 0
ktemp= 273.15

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



Generated by GNU Enscript 1.6.6.