Contents
- average3.py
- demo_str.py
- escape_practice.py
- input_demo.py
- sales_tax.py
- scale.py
average3.py 1/6
[top][prev][next]
# In-class Exercise: Average Three Numbers
# By CS111
# Prompt the user for input
num1=input("Enter the first number: ")
num2=input("Enter the second number: ")
num3=input("Enter the third number: ")
# Total the three numbers
total = num1 + num2 + num3
# Divide the total of the numbers by 3
avg = total/3.0
# Remember: could do type conversion instead of 3.0
# Need to make total a float, somehow
# Display output to the user
print "The average is", avg
demo_str.py 2/6
[top][prev][next]
# Demonstrate long strings, escape sequences
# by Sara Sprenkle
#
string = """This is a long string.
Like, really long."""
print string
print "To print a \\, you must use \"\\\\\""
print "I could print more after this...",
print "See?"
escape_practice.py 3/6
[top][prev][next]
# Practice with escape sequences
# CS111
#
# Print: To print a tab, you must use '\t'.
print "To print a tab, you must use a '\\t'."
print "To print a tab, you must use a \'\\t\'."
# Print: I said, "How are you?"
print "I said, \"How are you?\""
print 'I said, "How are you?"'
input_demo.py 4/6
[top][prev][next]
# Demonstrate numeric and string input
# by Sara Sprenkle for CS111 on 1/8/08
color = raw_input("What is your favorite color? ")
print "Cool! My favorite color is _light_", color, "!"
scale = input("On a scale of 1 to 10, how much do you like Matt Damon? ")
print "Cool! I like him", scale*1.8, "much!" # always likes him more
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=.045 # the sales tax in VA
value = input("How much does your item cost? ")
tax = value * (1+SALES_TAX)
print "Your item that cost ($%.2f)" % value
print "costs $%.2f with tax"%tax
scale.py 6/6
[top][prev][next]
# Demonstrate use of constants, string concatenation
# by Sara Sprenkle for class on 09/14/07
#
SCALE_MIN=1
SCALE_MAX=100
prompt = "On a scale of " + str(SCALE_MIN) + " to " + str(SCALE_MAX) + ", how much do you like Matt Damon?"
scale = input(prompt)
print scale,"?!? That's more than I do."
scale = input(prompt)
print "Nah,", scale, "is much too low."
Generated by GNU enscript 1.6.4.