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, with some bonus examples
# CSCI111

x = 10
y = 3.5
z = "apple"
print("{:6d}".format(x))
print("{:6.2f}".format(y))
print("{:06.2f}".format(y))
print("{:+6.2f}".format(y))
print("{:6.2f}".format(x))
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.")

# Version 3
print("\nVersion 3:")
formattedValue = "${:.2f}".format(value)
formattedTax = "${:.2f}".format(tax)
print("Your item that cost", formattedValue, costs, formattedTax, "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 $", round(with_tax, 2), "with tax.")

./temp_table.py 4/4

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

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

"""
Desired output: 

Temp F      Temp C      Temp K
------      ------      ------
-459.7      -273.1         0.0
   0.0       -17.8       255.2
  32.0         0.0       273.1

"""

# display the labels

# template for the headings:
headings_template = "{:>6s} {:>12s} {:>12s}"
print(headings_template.format("Temp F", "Temp C", "Temp K"))
print(headings_template.format("-"*6, "-"*6, "-"*6))



# display the data

# template for the data:
data_template = "{:6.1f} {:12.1f} {:12.1f}"

ftemp = -459.67
ctemp = -255.2
ktemp = 0

print(data_template.format(ftemp, ctemp, ktemp))


ftemp = 0.0
ctemp = -17.7778
ktemp = 255.372

print(data_template.format(ftemp, ctemp, ktemp))


ftemp = 32
ctemp = 0
ktemp= 273.15

print(data_template.format(ftemp, ctemp, ktemp))



Generated by GNU Enscript 1.6.5.90.