3.1.11. Questions, Examples and Projects

List of Questions:

List of Examples:

List of Projects:

3.1.11.1. Questions 1: Print, Expressions and Variables

3.1.11.1.1. Question 1

Which of the following are syntactically correct strings?

  • 'She shouted "Hello!" very loudly.'

  • "Hello, world."

  • [Hello] (not a string - string must use quotation marks)

  • Hello (not a string - string must use quotation marks)

  • 'This is great!'

3.1.11.1.2. Question 2

To display a value in the console, what Python keyword do you use?

print

3.1.11.1.3. Question 3

In the following code, the one line starting with #. What does this line mean to Python?:

tax_rate = 0.15
income = 40000
deduction = 10000
# Calculate income taxes
tax = (income - deduction) * tax_rate
print tax
  • This text is used as a file name for the code.

  • The text is stored in a special variable called #.

  • This is a syntax error.

  • This text is printed on the console.

  • This is a comment aimed at the human reader. Python ignores such comments.

3.1.11.1.4. Question 4

Which of the following arithmetic expressions are syntactically correct?

  • 7 / +4

  • 9 + * 4 (syntax error - two operators are together)

  • (8 + (1 + (2 * 4) - 3))

  • (7 - 2) / (3 ** 2)

  • 9 - (2 - (4 * 3) (syntax error - parenthesis not closed)

3.1.11.1.5. Question 5

You would like to make it so that the variable ounces has the value 16, thus representing one pound. What simple Python statement will accomplish this?

  • 16 = ounces (syntax error)

  • ounces = 16

  • ounces := 16 (syntax error)

3.1.11.1.6. Question 6

A gram is equal to 0.035274 ounces. Assume that the variable mass_in_ounces has a value representing a mass in ounces. Which arithmetic expressions below using the variable mass_in_ounces represent the equivalent mass in grams?

  • 0.035274 * mass_in_ounces

  • mass_in_ounces / 0.035274 (correct)

  • 0.035274 / mass_in_ounces

  • mass_in_ounces * 0.035274

3.1.11.1.7. Question 7

Which of the following can be used as a variable name?

  • ounces

  • 16ounces (syntax error - cannot start variable with digits)

  • number123

  • my-number (syntax error - dash is a minus sign)

  • MYnumber (ok but unconventional)

  • __number__ (ok but only for special variables)

3.1.11.1.8. Question 8

Assume you have values in the variables x and y. Which statement(s) would result in x having the sum of the current values of x and y?

  • x += y (this)

  • x = x + y (this)

  • y += x

  • x += x + y

3.1.11.1.9. Question 9

Python file names traditionally end in what characters after a period? Don’t include the period.

py

3.1.11.1.10. Question 10

We encourage you to save your Python files where?

  • Nowhere — Python automatically saves your files for you

  • On your computer only

  • In “the cloud” and on your computer

  • In “the cloud” only

3.1.11.2. Questions 2: Functions, Logic and Conditionals

3.1.11.2.1. Question 1

An if statement can have how many elif parts?

  • Unlimited, i.e., 0 or more

  • 1

  • 0

3.1.11.2.2. Question 2

Consider the Boolean expression not (p or not q). Give the four following values:

  • the value of the expression when p is True, and q is True = False

  • the value of the expression when p is True, and q is False = False

  • the value of the expression when p is False, and q is True = True

  • the value of the expression when p is False, and q is False = False

3.1.11.2.3. Question 3

Given a non-negative integer n, which of the following expressions computes the ten’s digit of n? For example, if n is 123, then we want the expression to evaluate to 2.

  • (n - n % 10) / 10

  • (n // 10) % 10

  • ((n - n % 10) % 100) / 10

3.1.11.2.4. Question 4

The function calls random.randint(0, 10) and random.randrange(0, 10) generate random numbers in different ranges. What number can be generated by one of these functions, but not the other?

10

By the way, we (and most Python programmers) always prefer to use random.randrange() since it handles numerical ranges in a way that is more consistent with the rest of Python.

3.1.11.2.5. Question 5

Implement the mathematical function \(f(x) = -5 x^5 + 69 x^2 - 47\) as a Python function. Then use Python to compute the function values \(f(0), f(1), f(2), and f(3)\). Enter the maximum of these four numbers.

69

def quartic(x):
    return -5*x**5 + 69*x**2 - 47

quartic(0)
quartic(1)
quartic(2)
quartic(3)
-47
17
69
-641

3.1.11.2.6. Question 6

When investing money, an important concept to know is compound interest. The equation \(FV = PV (1+rate)^{periods}\) relates the following four quantities.

  • The present value (PV) of your money is how much money you have now.

  • The future value (FV) of your money is how much money you will have in the future.

  • The nominal interest rate per period (rate) is how much interest you earn during a particular length of time, before accounting for compounding. This is typically expressed as a percentage.

  • The number of periods (periods) is how many periods in the future this calculation is for.

Before submitting your answer, test your function on the following example. future_value(500, .04, 10, 10) should return 745.317442824.

1061.83480113

def future_value(present_value, annual_rate, periods_per_year, years):
    rate_per_period = annual_rate / periods_per_year
    periods = periods_per_year * years

    return present_value*(1 + rate_per_period)**(periods_per_year*years)

print "$1000 at 2% compounded 365 times per year for 3 years yields $", future_value(1000, .02, 365, 3)
$1000 at 2% compounded 365 times per year for 3 years yields $ 1061.83480113

3.1.11.2.7. Question 7

There are several ways to calculate the area of a regular polygon. Given the number of sides, n, and the length of each side, s, the polygon’s area is

$$¼ n s^2 / tan(π/n)$$

For example, a regular polygon with 5 sides, each of length 7 inches, has area 84.3033926289 square inches.

Write a function that calculates the area of a regular polygon, given the number of sides and length of each side. Submit the area of a regular polygon with 7 sides each of length 3 inches. Enter a number (and not the units) with at least four digits of precision after the decimal point.

Note that the use of inches as the unit of measurement in these examples is arbitrary. Python only keeps track of the numerical values, not the units.

32.705211996014306

import math

def polygon_area(no_of_sides, side_length):
    return (0.25*no_of_sides*side_length**2) / tan(math.pi/no_of_sides)

polygon_area(5,7)
polygon_area(7,3)
84.303392628859385
32.705211996014306

3.1.11.2.8. Question 8

Running the following program results in the error SyntaxError: bad input on line 8 (‘return’). Which of the following describes the problem?:

def max_of_2(a, b):
    if a > b:
        return a
    else:
        return b

def max_of_3(a, b, c):
return max_of_2(a, max_of_2(b, c))
  • Misspelled keyword

  • Incorrect indentation

  • Misspelled function name

  • Wrong number of arguments in function call

  • Extra parenthesis

  • Misspelled variable name

  • Missing colon

  • Missing parenthesis

3.1.11.2.9. Question 9

The following code has a number of syntactic errors in it. The intended math calculations are correct, so the only errors are syntactic. Fix the syntactic errors.

Once the code has been fully corrected, it should print out two numbers. The first should be 1.09888451159. Submit the second number printed in CodeSkulptor. Provide at least four digits of precision after the decimal point.:

define project_to_distance(point_x point_y distance):
    dist_to_origin = math.square_root(pointx ** 2 + pointy ** 2)
     scale == distance / dist_to_origin
    print point_x * scale, point_y * scale

project-to-distance(2, 7, 4)

3.84609579056

import math

def project_to_distance(point_x, point_y, distance):
    dist_to_origin = math.sqrt(point_x ** 2 + point_y ** 2)
    scale = distance / dist_to_origin
    print point_x * scale, point_y * scale

project_to_distance(2, 7, 4)
1.09888451159 3.84609579056

3.1.11.3. Examples 1: Expressions

# Compute number of feet corresponding to number of miles

#####
# Miles to feet conversion expression
#####
print 13 * 5280

#####
# Expected output
#####
# 68640


# Compute the number of seconds in a given number of hours, minutes, and seconds.

###################################################
# Hours, minutes, and seconds to seconds conversion formula
# Student should enter statement on the next line.

print 7 * 60 * 60 + 21 * 60 + 37

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#26497


# Compute the length of a rectangle's perimeter, given its width and height.

###################################################
# Rectangle perimeter formula
# Student should enter statement on the next line.

w = 4
h = 7

print 2 * w + 2 * h

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#22


# Compute the area of a rectangle, given its width and height.

###################################################
# Rectangle area formula
# Student should enter statement on the next line.

w = 4
h = 7

print w * h

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#28


# Compute the circumference of a circle, given the length of its radius.

###################################################
# Circle circumference formula
# Student should enter statement on the next line.

pi = 3.14
r = 8 

print 2 * pi * r

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#50.24


# Compute the area of a circle, given the length of its radius.

###################################################
# Circle area formula
# Student should enter statement on the next line.
pi = 3.14
r = 8

print pi * r ** 2

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#200.96


# Compute the future value of a given present value, annual rate, and number of years.

###################################################
# Future value formula
# Student should enter statement on the next line.

1000 * (1 + 0.01 * 7) ** 10

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#1967.15135729


# Compute a name tag, given the first and last name.

###################################################
# Name tag formula
# Student should enter statement on the next line.

print "My name is " + "Joe" + " Warren"

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#My name is Joe Warren.


# Compute the statement about a person's name and age, given the person's name and age.

###################################################
# Name and age formula
# Student should enter statement on the next line.

print "Joe Warren is " + str(52) + " years old"

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#Joe Warren is 52 years old.


# Compute the distance between the points (x0, y0) and (x1, y1).

###################################################
# Distance formula
# Student should enter statement on the next line.

# Hint: You need to use the power operation ** .

x0 = 2
y0 = 2

x1 = 5
y1 = 6

print ((x0 - x1)**2 + (y0 - y1)**2)**0.5
###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#5.0




3.1.11.4. Examples 2: Assignment and Variables


# Compute the number of feet corresponding to a number of miles.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#miles = 13


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#miles = 57


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
miles = 82.67


###################################################
# Miles to feet conversion formula
# Student should enter formula on the next line.

feet = 5280 * miles

###################################################
# Test output
# Student should not change this code.

print str(miles) + " miles equals " + str(feet) + " feet."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#13 miles equals 68640 feet.

# Test 2 output:
#57 miles equals 300960 feet.

# Test 3 output:
#82.67 miles equals 436497.6 feet.



# Compute the number of seconds in a given number of hours, minutes, and seconds.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#hours = 7
#minutes = 21
#seconds = 37


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#hours = 10
#minutes = 1
#seconds = 7


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
hours = 1
minutes = 0
seconds = 1


###################################################
# Hours, minutes, and seconds to seconds conversion formula
# Student should enter formula on the next line.

total_seconds = hours * 60 * 60 + minutes * 60 + seconds

###################################################
# Test output
# Student should not change this code.

print str(hours) + " hours, " + str(minutes) + " minutes, and",
print str(seconds) + " seconds totals to " + str(total_seconds) + " seconds."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
# Test 1 output:
#7 hours, 21 minutes, and 37 seconds totals to 26497 seconds.

# Test 2 output:
#10 hours, 1 minutes, and 7 seconds totals to 36067 seconds.

# Test 3 output:
#1 hours, 0 minutes, and 1 seconds totals to 3601 seconds.



# Compute the length of a rectangle's perimeter, given its width and height.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#width = 4
#height = 7


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#width = 7
#height = 4


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
width = 10
height = 10


###################################################
# Rectangle perimeter formula
# Student should enter formula on the next line.

perimeter = 2 * width + 2 * height

###################################################
# Test output
# Student should not change this code.

print "A rectangle " + str(width) + " inches wide and " + str(height),
print "inches high has a perimeter of " + str(perimeter) + " inches."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#A rectangle 4 inches wide and 7 inches high has a perimeter of 22 inches.

# Test 2 output:
#A rectangle 7 inches wide and 4 inches high has a perimeter of 22 inches.

# Test 3 output:
#A rectangle 10 inches wide and 10 inches high has a perimeter of 40 inches.



# Compute the area of a rectangle, given its width and height.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#width = 4
#height = 7


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#width = 7
#height = 4


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
width = 10
height = 10


###################################################
# Rectangle area formula
# Student should enter formula on the next line.

area = width * height

###################################################
# Test output
# Student should not change this code.

print "A rectangle " + str(width) + " inches wide and " + str(height),
print "inches high has an area of " + str(area) + " square inches."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#A rectangle 4 inches wide and 7 inches high has an area of 28 square inches.

# Test 2 output:
#A rectangle 7 inches wide and 4 inches high has an area of 28 square inches.

# Test 3 output:
#A rectangle 10 inches wide and 10 inches high has an area of 100 square inches.



# Compute the circumference of a circle, given the length of its radius.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.
PI = 3.14

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#radius = 8


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#radius = 3


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
radius = 12.9


###################################################
# Circle circumference formula
# Student should enter formula on the next line.

circumference = 2 * PI * radius 

###################################################
# Test output
# Student should not change this code.

print "A circle with a radius of " + str(radius),
print "inches has a circumference of " + str(circumference) + " inches."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#A circle with a radius of 8 inches has a circumference of 50.24 inches.

# Test 2 output:
#A circle with a radius of 3 inches has a circumference of 18.84 inches.

# Test 3 output:
#A circle with a radius of 12.9 inches has a circumference of 81.012 inches.



# Compute the area of a circle, given the length of its radius.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.
PI = 3.14

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#radius = 8


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#radius = 3


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
radius = 12.9


###################################################
# Circle area formula
# Student should enter formula on the next line.

area = PI * radius ** 2

###################################################
# Test output
# Student should not change this code.

print "A circle with a radius of " + str(radius),
print "inches has an area of " + str(area) + " square inches."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#A circle with a radius of 8 inches has an area of 200.96 square inches.

# Test 2 output:
#A circle with a radius of 3 inches has an area of 28.26 square inches.

# Test 3 output:
#A circle with a radius of 12.9 inches has an area of 522.5274 square inches.



# Compute the future value of a given present value, annual rate, and number of years.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#present_value = 1000
#annual_rate = 7
#years = 10


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#present_value = 200
#annual_rate = 4
#years = 5


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
present_value = 1000
annual_rate = 3
years = 20


###################################################
# Future value formula
# Student should enter formula on the next line.

future_value = present_value * (1 + 0.01 * annual_rate) ** years

###################################################
# Test output
# Student should not change this code.

print "The future value of $" + str(present_value) + " in " + str(years),
print "years at an annual rate of " + str(annual_rate) + "% is $" + str(future_value) + "."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#The future value of $1000 in 10 years at an annual rate of 7% is $1967.15135729.

# Test 2 output:
#The future value of $200 in 5 years at an annual rate of 4% is $243.33058048.

# Test 3 output:
#The future value of $1000 in 20 years at an annual rate of 3% is $1806.11123467.



# Compute a name tag, given the first and last name.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#first_name = "Joe"
#last_name = "Warren"


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#first_name = "Scott"
#last_name = "Rixner"


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
first_name = "John"
last_name = "Greiner"


###################################################
# Name tag formula
# Student should enter formula on the next line.

name_tag = "My name is " + first_name + " " + last_name

###################################################
# Test output
# Student should not change this code.

print name_tag


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#My name is Joe Warren.

# Test 2 output:
#My name is Scott Rixner.

# Test 3 output:
#My name is John Greiner.



# Compute the statement about a person's name and age, given the person's name and age.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#name = "Joe Warren"
#age = 52


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#name = "Scott Rixner"
#age = 40


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
name = "John Greiner"
age = 46


###################################################
# Name and age formula
# Student should enter formula on the next line.

statement = name + " is " + str(age) + " years old"

###################################################
# Test output
# Student should not change this code.

print statement

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#Joe Warren is 52 years old.

# Test 2 output:
#Scott Rixner is 40 years old.

# Test 3 output:
#John Greiner is 46 years old.



# Compute the distance between the points (x0, y0) and (x1, y1).

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#x0 = 2
#y0 = 2
#x1 = 5
#y1 = 6


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#x0 = 1
#y0 = 1
#x1 = 2
#y1 = 2


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
x0 = 0
y0 = 0
x1 = 3
y1 = 4


###################################################
# Distance formula
# Student should enter formula on the next line.

# Hint: You need to use the power operation ** .

distance = ((x0 - x1)**2+(y0 - y1)**2)**0.5


###################################################
# Test output
# Student should not change this code.

print "The distance from (" + str(x0) + ", " + str(y0) + ") to", 
print "(" + str(x1) + ", " + str(y1) + ") is " + str(distance) + "."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#The distance from (2, 2) to (5, 6) is 5.0.

# Test 2 output:
#The distance from (1, 1) to (2, 2) is 1.41421356237.

# Test 3 output:
#The distance from (0, 0) to (3, 4) is 5.0.


# Compute the area of a triangle (using Heron's formula),
# given its side lengths.

###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

# Test 1 - Select the following lines and use ctrl+shift+k to uncomment.
#x0, y0 = 0, 0
#x1, y1 = 3, 4
#x2, y2 = 1, 1


# Test 2 - Select the following lines and use ctrl+shift+k to uncomment.
#x0, y0 = -2, 4
#x1, y1 = 1, 6
#x2, y2 = 2, 1


# Test 3 - Select the following lines and use ctrl+shift+k to uncomment.
x0, y0 = 10, 0
x1, y1 = 0, 0
x2, y2 = 0, 10


###################################################
# Triangle area (Heron's) formula
# Student should enter formulas on the next lines.

a = ((x0 - x1)**2+(y0 - y1)**2)**0.5
b = ((x0 - x2)**2+(y0 - y2)**2)**0.5
c = ((x1 - x2)**2+(y1 - y2)**2)**0.5

s = 0.5 * (a + b + c)

area = (s * (s - a) * (s - b) * (s - c))**0.5

###################################################
# Test output
# Student should not change this code.

print "A triangle with vertices (" + str(x0) + "," + str(y0) + "),",
print "(" + str(x1) + "," + str(y1) + "), and",
print "(" + str(x2) + "," + str(y2) + ") has an area of " + str(area) + "."


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

# Test 1 output:
#A triangle with vertices (0,0), (3,4), and (1,1) has an area of 0.5.

# Test 2 output:
#A triangle with vertices (-2,4), (1,6), and (2,1) has an area of 8.5.

# Test 3 output:
#A triangle with vertices (10,0), (0,0), and (0,10) has an area of 50.




3.1.11.5. Examples 3: Functions


# Compute the number of feet corresponding to a number of miles.

###################################################
# Miles to feet conversion formula
# Student should enter function on the next lines.

def miles_to_feet(miles):
    return 5280 * miles


###################################################
# Tests
# Student should not change this code.

def test(miles):
	print str(miles) + " miles equals",
	print str(miles_to_feet(miles)) + " feet."

test(13)
test(57)
test(82.67)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#13 miles equals 68640 feet.
#57 miles equals 300960 feet.
#82.67 miles equals 436497.6 feet.


# Compute the number of seconds in a given number of hours, minutes, and seconds.

###################################################
# Hours, minutes, and seconds to seconds conversion formula
# Student should enter function on the next lines.

def total_seconds(hours, minutes, seconds):
    return hours * 60 * 60 + minutes * 60 + seconds

###################################################
# Tests
# Student should not change this code.

def test(hours, minutes, seconds):
	print str(hours) + " hours, " + str(minutes) + " minutes, and",
	print str(seconds) + " seconds totals to",
	print str(total_seconds(hours, minutes, seconds)) + " seconds."

test(7, 21, 37)
test(10, 1, 7)
test(1, 0, 1)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.


#7 hours, 21 minutes, and 37 seconds totals to 26497 seconds.
#10 hours, 1 minutes, and 7 seconds totals to 36067 seconds.
#1 hours, 0 minutes, and 1 seconds totals to 3601 seconds.


# Compute the length of a rectangle's perimeter, given its width and height.

###################################################
# Rectangle perimeter formula
# Student should enter function on the next lines.

def rectangle_perimeter(width, height):
    return 2 * width + 2 * height

###################################################
# Tests
# Student should not change this code.

def test(width, height):
	print "A rectangle " + str(width) + " inches wide and " + str(height),
	print "inches high has a perimeter of",
	print str(rectangle_perimeter(width, height)) + " inches."

test(4, 7)
test(7, 4)
test(10, 10)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#A rectangle 4 inches wide and 7 inches high has a perimeter of 22 inches.
#A rectangle 7 inches wide and 4 inches high has a perimeter of 22 inches.
#A rectangle 10 inches wide and 10 inches high has a perimeter of 40 inches.


# Compute the area of a rectangle, given its width and height.

###################################################
# Rectangle area formula
# Student should enter function on the next lines.

def rectangle_area(width, height):
    return width * height


###################################################
# Tests
# Student should not change this code.

def test(width, height):
	print "A rectangle " + str(width) + " inches wide and " + str(height),
	print "inches high has an area of",
	print str(rectangle_area(width, height)) + " square inches."

test(4, 7)
test(7, 4)
test(10, 10)

	
###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#A rectangle 4 inches wide and 7 inches high has an area of 28 square inches.
#A rectangle 7 inches wide and 4 inches high has an area of 28 square inches.
#A rectangle 10 inches wide and 10 inches high has an area of 100 square inches.


# Compute the circumference of a circle, given the length of its radius.

###################################################
# Circle circumference formula
# Student should enter function on the next lines.

import math

def circle_circumference(radius):
    return 2 * math.pi * radius

###################################################
# Tests
# Student should not change this code.

def test(radius):
	print "A circle with a radius of " + str(radius),
	print "inches has a circumference of",
	print str(circle_circumference(radius)) + " inches."

test(8)
test(3)
test(12.9)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#A circle with a radius of 8 inches has a circumference of 50.2654824574 inches.
#A circle with a radius of 3 inches has a circumference of 18.8495559215 inches.
#A circle with a radius of 12.9 inches has a circumference of 81.0530904626 inches.


# Compute the area of a circle, given the length of its radius.

###################################################
# Circle area formula
# Student should enter function on the next lines.

import math

def circle_area(radius):
    return math.pi * (radius ** 2)

###################################################
# Tests
# Student should not change this code.

def test(radius):
	print "A circle with a radius of " + str(radius),
	print "inches has an area of",
	print str(circle_area(radius)) + " square inches."

test(8)
test(3)
test(12.9)

###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#A circle with a radius of 8 inches has an area of 201.06192983 square inches.
#A circle with a radius of 3 inches has an area of 28.2743338823 square inches.
#A circle with a radius of 12.9 inches has an area of 522.792433484 square inches.


# Compute the future value of a given present value, annual rate, and number of years.

###################################################
# Future value formula
# Student should enter function on the next lines.

def future_value(present_value, annual_rate, years):
    return present_value * (1 + 0.01 * annual_rate) ** years

###################################################
# Tests
# Student should not change this code.

def test(present_value, annual_rate, years):
	"""Tests the future_value function."""
	
	print "The future value of $" + str(present_value) + " in " + str(years),
	print "years at an annual rate of " + str(annual_rate) + "% is",
	print "$" + str(future_value(present_value, annual_rate, years)) + "."


###################################################
# Tests
# Student should uncomment ONLY ONE of the following at a time.

test(1000, 7, 10)
test(200, 4, 5)
test(1000, 3, 20)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#The future value of $1000 in 10 years at an annual rate of 7% is $1967.15135729.
#The future value of $200 in 5 years at an annual rate of 4% is $243.33058048.
#The future value of $1000 in 20 years at an annual rate of 3% is $1806.11123467.


# Compute a name tag, given the first and last name.

###################################################
# Name tag formula
# Student should enter function on the next lines.

def name_tag(first_name, last_name):
    return "My name is " + first_name + " " + last_name + "."

###################################################
# Tests
# Student should not change this code.

def test(first_name, last_name):
	print name_tag(first_name, last_name)
	
test("Joe", "Warren")
test("Scott", "Rixner")
test("John", "Greiner")


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#My name is Joe Warren.
#My name is Scott Rixner.
#My name is John Greiner.


# Compute the statement about a person's name and age, given the person's name and age.

###################################################
# Name and age formula
# Student should enter function on the next lines.

def name_and_age(name, age):
    return name + " is " + str(age) + " years old."

###################################################
# Tests
# Student should not change this code.

def test(name, age):
	print name_and_age(name, age)
	
test("Joe Warren", 52)
test("Scott Rixner", 40)
test("John Greiner", 46)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#Joe Warren is 52 years old.
#Scott Rixner is 40 years old.
#John Greiner is 46 years old.


# Compute the distance between the points (x0, y0) and (x1, y1).

###################################################
# Distance formula
# Student should enter function on the next lines.

# Hint: You need to use the power operation ** .

def point_distance(x0, y0, x1, y1):
    return sqrt((x0 - x1)**2 + (y0 - y1)**2)

###################################################
# Tests
# Student should not change this code.

def test(x0, y0, x1, y1):
	print "The distance from (" + str(x0) + ", " + str(y0) + ") to",
	print "(" + str(x1) + ", " + str(y1) + ") is",
	print str(point_distance(x0, y0, x1, y1)) + "."

test(2, 2, 5, 6)
test(1, 1, 2, 2)
test(0, 0, 3, 4)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#The distance from (2, 2) to (5, 6) is 5.0.
#The distance from (1, 1) to (2, 2) is 1.41421356237.
#The distance from (0, 0) to (3, 4) is 5.0.


# Compute the area of a triangle (using Heron's formula),
# given its side lengths.

###################################################
# Triangle area (Heron's) formula
# Student should enter function on the next lines.
# Hint:  Also define point_distance as use it as a helper function.

def triangle_area(x0, y0, x1, y1, x2, y2):
    a = point_distance(x0, y0, x1, y1)
    b = point_distance(x1, y1, x2, y2)
    c = point_distance(x2, y2, x0, y0)
    s = (a + b + c) / 2
    return sqrt(s * (s - a) * (s - b) * (s - c))

###################################################
# Tests
# Student should not change this code.

def test(x0, y0, x1, y1, x2, y2):
	print "A triangle with vertices (" + str(x0) + "," + str(y0) + "),",
	print "(" + str(x1) + "," + str(y1) + "), and",
	print "(" + str(x2) + "," + str(y2) + ") has an area of",
	print str(triangle_area(x0, y0, x1, y1, x2, y2)) + "."

test(0, 0, 3, 4, 1, 1)
test(-2, 4, 1, 6, 2, 1)
test(10, 0, 0, 0, 0, 10)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#A triangle with vertices (0,0), (3,4), and (1,1) has an area of 0.5.
#A triangle with vertices (-2,4), (1,6), and (2,1) has an area of 8.5.
#A triangle with vertices (10,0), (0,0), and (0,10) has an area of 50.


# Compute and print tens and ones digit of an integer in [0,100).

###################################################
# Digits function
# Student should enter function on the next lines.

def print_digits(number):
    tens = number // 10
    ones = number % 10
    print "The tens digit is " + str(tens) + ",", 
    print "and the ones digit is " + str(ones) + "."

	
###################################################
# Tests
# Student should not change this code.
	
print_digits(42)
print_digits(99)
print_digits(5)


###################################################
# Expected output
# Student should look at the following comments and compare to printed output.

#The tens digit is 4, and the ones digit is 2.
#The tens digit is 9, and the ones digit is 9.
#The tens digit is 0, and the ones digit is 5.


# Compute and print powerball numbers.

###################################################
# Powerball function
# Student should enter function on the next lines.

import random

def different_numbers(no1, no2, no3, no4, no5, no6, no7):
    number1 = ((no1 != no2) and 
               (no1 != no3) and 
               (no1 != no4) and 
               (no1 != no5) and 
               (no1 != no6) and 
               (no1 != no7)) 
    number2 = ((no2 != no3) and 
               (no2 != no4) and 
               (no2 != no5) and 
               (no2 != no6) and 
               (no2 != no7))
    number3 = ((no3 != no4) and 
               (no3 != no5) and 
               (no3 != no6) and  
               (no3 != no7))
    number4 = ((no4 != no5) and 
               (no4 != no6) and 
               (no4 != no7))
    number5 = ((no5 != no6) and 
               (no5 != no7))
    number6 = (no6 != no7)
    
    return (number1 and number2 and number3 and
            number4 and number5 and number6)
    
def powerball():
    while True:
        ball_one = random.randrange(1, 60)
        ball_two = random.randrange(1, 60)
        ball_three = random.randrange(1, 60)
        ball_four = random.randrange(1, 60)
        ball_five = random.randrange(1, 60)
        ball_six = random.randrange(1, 60)
        ball_powerball = random.randrange(1, 36)
        
        if different_numbers(ball_one, ball_two, ball_three, 
                             ball_four, ball_five, ball_six,
                             ball_powerball):
            break

    print "Today's numbers are",
    print str(ball_one) + ",",
    print str(ball_two) + ",",
    print str(ball_three) + ",",
    print str(ball_four) + ",",
    print str(ball_five) + ",",
    print str(ball_six) + ".",
    print "The Powerball number is",
    print str(ball_powerball) + "."

	
###################################################
# Tests
# Student should not change this code.
	
powerball()
powerball()
powerball()




3.1.11.6. Project 1: Hello World!

# 1. Program doesn't print an error
# 2. Program prints output to console
# 3. Program prints desired message

print "We want... a shrubbery"

3.1.11.7. Project 2: Rock, Paper, Scissors, Lizard, Spock


# Testing template for name_to_number()

###################################################
# Copy and paste your definition of name_to_number() here

def name_to_number(name):
    if(name == "rock"):
        return 0
    elif(name == "Spock"):
        return 1
    elif(name == "paper"):
        return 2
    elif(name == "lizard"):
        return 3
    elif(name == "scissors"):
        return 4
    else:
        pass
  #      print "Sorry, your choice is not one of",
  #      print "rock, paper, scissors, lizard or Spock"

###################################################
# Test calls to name_to_number()
print name_to_number("rock")
print name_to_number("Spock")
print name_to_number("paper")
print name_to_number("lizard")
print name_to_number("scissors")
print name_to_number("scissfors")


###################################################
# Output to the console should have the form:
# 0
# 1
# 2
# 3
# 4

# Testing template for number_to_name()

###################################################
# Copy and paste your definition of number_to_name() here

def number_to_name(number):
    if(number == 0):
        return "rock"
    elif(number == 1):
        return "Spock"
    elif(number == 2):
        return "paper"
    elif(number == 3):
        return "lizard"
    elif(number == 4):
        return "scissors"
    else:
        pass

###################################################
# Test calls to number_to_name()
print number_to_name(0)
print number_to_name(1)
print number_to_name(2)
print number_to_name(3)
print number_to_name(4)
print number_to_name(5)


###################################################
# Output to the console should have the form:
# rock
# Spock
# paper
# lizard
# scissors



def verb(winning_choice, difference):
    if(winning_choice == "Spock"):
        if(abs(difference) == 3):
            return " smashes "
        elif(abs(difference) == 1):
            return " vaporises "
        
    elif(winning_choice == "lizard"):
        if(abs(difference) == 2):
            return " poisons "
        elif(abs(difference) == 1):
            return " eats "
        
    elif(winning_choice == "paper"):
        if(abs(difference) == 2):
            return " covers "
        elif(abs(difference) == 1):
            return " disproves "  
        
    elif(winning_choice == "rock"):
        if(abs(difference) == 4):
            return " blunts "
        elif(abs(difference) == 3):
            return " crushes "     
    elif(winning_choice == "scissors"):
        if(abs(difference) == 1):
            return " decapitate "
        elif(abs(difference) == 2):
            return " cuts "           


# Testing template for rpsls(player_choice)

###################################################

import random

def rpsls(player_choice):
    print
    print "You have chosen " + player_choice
    player_number = name_to_number(player_choice)
#    print "This is number " + str(player_number)
    
    if(player_number != None): 
        computer_number = random.randrange(0,5)
        computer_choice = number_to_name(computer_number)
        print "I have chosen " + computer_choice
#        print "This is number " + str(computer_number)
        difference = computer_number - player_number
        remainder = difference % 5
    
        if(remainder == 0):
            print "It's a draw!"
        elif(remainder == 1 or remainder == 2):
            print "I win -", 
            winning_verb = verb(computer_choice, difference)
            print computer_choice + winning_verb + player_choice + "!"
        elif (remainder == 3 or remainder == 4):
            print "You win -",
            winning_verb = verb(player_choice, difference)
            print player_choice + winning_verb + computer_choice + "!"
    else:
        print "Sorry, your choice was not one of",
        print "rock, paper, scissors, lizard or Spock."
        print "Please try again."
###################################################
# Test calls to number_to_name()
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
rpsls("scissfors")

###################################################
# Output to the console should have the form:

#You have chosen rock
#I have chosen ???
# You/I win!

#You have chosen Spock
#I have chosen ???
# You/I win!

#You have chosen paper
#I have chosen ???
# You/I win!

#You have chosen lizard
#I have chosen ???
# You/I win!

#You have chosen scissors
#I have chosen ???
# You/I win!


print 4 % 5 # player wins = 4

print 3 % 5 # player wins = 3
#Spock smashes scissors

print 2 % 5 # computer wins = 2
#Lizard poisons Spock

print 1 % 5 # computer wins = 1
#Spock vaporises rock
#Paper disproves Spock

print 0 % 5 #draw

print -1 % 5 # player wins = 4
#Spock vaporises rock
#Paper disproves Spock

print -2 % 5 # player wins = 3
#Lizard poisons Spock

print -3 % 5 # computer wins = 2 
#Spock smashes scissors

print -4 % 5 #computer wins = 1