Contents

  1. ./format_examples.py
  2. ./sales_tax2.py
  3. ./sales_tax.py

./format_examples.py 1/3

[
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))
print("*{:5d} {:<7.3f}*".format(x,y))


./sales_tax2.py 2/3

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

[
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.")

Generated by GNU Enscript 1.6.5.90.