0% found this document useful (0 votes)
2 views6 pages

CPT Practice 1 - Program Code

The document outlines a Python program called 'Length and Width Game' designed to help users explore geometric calculations involving length and width. It includes various functions to calculate the area and perimeter of rectangles, squares, and circles, as well as the volume and surface area of cubes. The program allows users to input values and choose calculations through a menu-driven interface.

Uploaded by

s9131297
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

CPT Practice 1 - Program Code

The document outlines a Python program called 'Length and Width Game' designed to help users explore geometric calculations involving length and width. It includes various functions to calculate the area and perimeter of rectangles, squares, and circles, as well as the volume and surface area of cubes. The program allows users to input values and choose calculations through a menu-driven interface.

Uploaded by

s9131297
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#1st Period

import math

print("Final Project - Length & Width Game")


print("----------------------------")
# ==============================================
# Program: The Length and Width Game
# Subtitle: What Can I Do with Length and Width?
# ==============================================
# Purpose: To help users explore how length and
# width values can be used in various
# geometric calculations.
# Teacher: Ms. Miller
# Author: John A.
# ==============================================

#-----------------------------------------------
# PC has a Purpose
# PC has functionality(does something)
# Program Code is an Algorithm

# Variables that will be my parameters


length = 0
width = 0
choice = 0

# length and width should be initialized as a float and taken in as a


float so that it can that the arithmetic is more precise.

# Program control changes based on the user's menu choice


def call_list():
print("\nChoose a function:")
print("(1) Area of Rectangle")
print("(2) Perimeter of Rectangle")
print("(3) Area of Maximum Square inside Rectangle")
print("(4) Area of Circle inside Max Square")
print("(5) Perimeter of Right Triangle in Rectangle")
print("(6) Perimeter of Right Triangle in Square")
print("(7) Area of Square ")
print("(8) Perimeter of Square ")
print("(9) Surface Area of Cube ")
print("(10) Volume of Cube ")
print("(11) Area of the unwatered crop in a crop circle inside of
a sq field")
print()

# To input a valid number for the length and width - Class Code
def get_valid_input(param):
while True:
user_input = input("Please input your "+ param +" and then hit
enter: ")
try:
number = float(user_input)
if 1 <= number <= 1000:
return number
else:
print("I need a valid number between 1 and 1000")
except ValueError:
print("I need a number")

#--------------------
# define the functions - Student-created function with parameters
#--------------------

#does not matter if l==w or l>w , or l<w


def area_rect(length, width):
return (length * width)

#does not matter if l==w or l>w , or l<w


def perim_rect(length, width):
return (2 * (length + width))

#does not matter if l==w or l>w , or l<w


def area_max_square_in_rect(length, width):
if length < width:
return length ** 2
else:
return width ** 2

#does not matter if l==w or l>w , or l<w


def area_circle_in_max_square(length, width):
if length < width:
return [Link] * (length / 2) ** 2
else:
return [Link] * (width / 2) ** 2

#does not matter if l==w or l>w , or l<w


def perim_of_right_tri_in_rect(length, width):
return ((length + width) + (length**2 + width**2)**0.5)

#matters if l==w
def perim_of_right_tri_in_sq(length, width):
if length == width:
return ((length + width) + (2*(length**2))**0.5)
else:
return None

#matters if l==w
def area_square(length, width):
if length == width:
return (length * length)
else:
return None

#matters if l==w
def perim_square(length, width):
if length == width:
return (4 * length)
else:
return None
#matters if l==w
def surf_cube(length, width):
if length == width:
return (6*(length * length))
else:
return None

#matters if l==w
def vol_cube(length, width):
if length == width:
return (width**3)
else:
return None

#matters if l==w
def unwatered_crop_area(length, width):
if length == width:
r = (length/2)
area_of_the_circle = [Link]*(r**2)
return ((length * length) - area_of_the_circle)
else:
return None

#---------------------------
# input - length and width
#--------------------------

# Asking the length and width using the get_valid_input function


print("Hello, welcome to the 'Fun with Length and Width Game'")
length = get_valid_input("length")
width = get_valid_input("width")

# Main program loop.


# This while loop allows the user to repeatedly choose calculations
until they decide to stop.
# Also incorporates many if else statements
while True:
call_list()
choice = int(input("Choose a number for a function to be used: "))
if choice == 1:
print(f"Area: {area_rect(length, width)} square units")
elif choice == 2:
print(f"Perimeter: {perim_rect(length, width)} linear units")
elif choice == 3:
print(f"Max square area: {area_max_square_in_rect(length,
width)} square units")
elif choice == 4:
print(f"Circle area: {area_circle_in_max_square(length,
width)} square units")
elif choice == 5:
print(f"Perimeter of Right Triangle:
{perim_of_right_tri_in_rect(length, width)} linear units")
elif choice == 6:
answer = perim_of_right_tri_in_sq(length, width)
print(f"Perimeter of Right Triangle: {answer} linear units" if
answer else "Not a square.")
elif choice == 7:
answer = area_square(length, width)
print(f"Area of Square: {answer} square units" if answer else
"Not a square.")
elif choice == 8:
answer = perim_square(length, width)
print(f"perimeter of Square: {answer} linear units" if answer
else "Not a square.")
elif choice == 9:
answer = surf_cube(length, width)
print(f"Surface area of cube: {answer} square units" if answer
else "Not a cube.")
elif choice == 10:
answer = vol_cube(length, width)
print(f"Volume of cube: {answer} cubic units" if answer else
"Not a cube.")
elif choice == 11:
answer = unwatered_crop_area(length, width)
print(f"Unwatered Crop Area: {answer} square units" if answer
else "Not a square.")
else:
print("Invalid choice.")

again = input("\nContinue? (y/n): ")


if again != "y":
break

#-----------------------------------
# Formulas in pseudocode
#-----------------------------------

# The formula source is from Class Notes


# 1. area of a rectangle different l w
# area_rect_diff_l_w = (length * width)
# 2. perimeter of a rectangle different l w
# perim_rect_diff_l_W = 2(length + width)
# 3. max area of a square in a rectangle [if l< w, l > w]
# area_of_the_max_square_l<w = length ** 2
# area_of_the_max_square_l>w = width ** 2
# 4. area of a max circle in the max square [if l < w, l > w]
# area_max_circle_in_max_sq_l = PI*(length/2)**2 #PIrsquared
# area_max_circle_in_max_sq_w = PI*(width/2)**2 #PIrsquared
# 5. perimeter of a right triangle in a rectangle different l w
# perim_rt_tri_in_rect_diff_l_w = (length + width) + (length**2 +
width**2)**0.5 #length + width + hypotenuse
# 6. perimeter of a right triangle in a square [if l < w, l > w]
# perim_rt_tri_insq = (length + width) + (2*(length**2))**0.5
#length + width + hypotenuse
# perim_rt_tri_insq = (length + width) + (2*(width**2))**0.5
#length + width + hypotenuse
# 7. if l == w area of a square
# area_square_l=w = (length * length)
# 8. if l == w perimeter of a square
#perim_sq = (4 * length)
# 9. if l == w surface area of a cube
# surface_area_cube_l=w = (6*(area_square_l=w)
# 10. if l == w volume of a cube
# vol_cube_l=w = (width**3)
# 11. area of the unwatered crop in a crop circle inside of a square
field. if l == w.
# r = (length/2)
# area_of_the_circle = PI*(r*r) #r squared could be (r**2)
# area_of_unwatered_crop = (area_square_l=w) - area_of_the_circle)

#------------------------------------------------

You might also like