Just For Fun 04: Area of a Square and Function Calls
Exploring Python Functions and Iteration
What this code does: This code defines a function squareArea(s) that calculates the area of a square given its side length s. The main() function acts as a driver: it prints the area for a single value, then iterates over a list of test values (including integers, zero, negative numbers, and a complex number) and prints the area for each. Function calls: - squareArea(s) is called inside main() for each value in testValues_list and for the initial sideLength. - main() is called at the end to start the program.
Example Code:
def squareArea(s: float ) -> float:
""" determine area of square"""
return s*s # area of square is s*s
# end of squareArea()
def main() -> None:
""" driver function """
sideLength = 5
# Testing value
print(f"Length {sideLength}")
print(f" Area: {squareArea(sideLength)}")
# These inputs work
testValues_list =[2,0,-3,2 + 5j]
# why will these inputs not work?
# testValues_list =[True, "radius"]
print("\n Iterating over the list.")
for val in testValues_list: #iteration
print(f" Length {val}, Area: {squareArea(val)}")
# end main()
main() # call the driver function
Loading Python interpreter…