Contents
- ./format_examples.py
- ./sales_tax2.py
- ./sales_tax.py
- ./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.")
# 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
print("{:>6s} {:>11s} {:>11s}".format("Temp F", "Temp C", "Temp K"))
print("{:>6s} {:>11s} {:>11s}".format("-"*6, "-"*6, "-"*6))
# probably should make the template string a variable so that it is easier
# to update just once.
ftemp = -459.67
ctemp = -273.15
ktemp=0
print("{:6.1f} {:11.1f} {:11.1f}".format(ftemp, ctemp, ktemp))
ftemp = 0
ctemp = -17.77778
ktemp= 255.222
print("{:6.1f} {:11.1f} {:11.1f}".format(ftemp, ctemp, ktemp))
ftemp = 32
ctemp = 0
ktemp= 273.15
print("{:6.1f} {:11.1f} {:11.1f}".format(ftemp, ctemp, ktemp))
Generated by GNU Enscript 1.6.5.90.