Contents

  1. circleArea_original.py
  2. circleArea.py
  3. practice1.py
  4. practice2.py
  5. practice3.py

circleArea_original.py 1/5

[
top][prev][next]
# This program finds the area of a circle, given user input
# for the radius of the circle
# original version.
# by CSCI111

import math

print("This program calculates the area of a circle.")
print()

radius=float(input("Enter circle's radius: "))

area=math.pi*radius**2

print()
print("The area of the circle is", round(area,3))

circleArea.py 2/5

[
top][prev][next]
# This program finds the area of a circle, given user input
# for the radius of the circle.
# Refactored to have functions
# by CSCI111

import math
import test

def main():
	print("This program calculates the area of a circle.")
	print()
	
	user_radius=float(input("Enter circle's radius: "))
	
	area=calculateAreaOfCircle(user_radius)
	
	print()
	print("The area of the circle is", round(area,3))


def calculateAreaOfCircle(radius):
	"""
	returns the area of the circle
	radius: the circle's radius as a non-negative float
	"""
	area=math.pi*radius**2
	return area
	
	
	
def testCalculateAreaOfCircle():
	test.testEqual(calculateAreaOfCircle(1), 3.14159)

testCalculateAreaOfCircle()
#main()

practice1.py 3/5

[
top][prev][next]
# Exercising your knowledge of variable scope.
#

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)

def square(n):
    return n * n

main()

practice2.py 4/5

[
top][prev][next]
# Exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", squared)
    print("The original num was", n)

def square(n):
    return n * n

main()

practice3.py 5/5

[
top][prev][next]
# Further exercising your knowledge of variable scope.

def main():
    num = eval(input("Enter a number to be squared: "))
    squared = square(num)
    print("The square is", computed)
    print("The original num was", num)

def square(n):
    computed = n*n
    return computed

main()

Generated by GNU Enscript 1.6.6.