0% found this document useful (0 votes)
31 views4 pages

AP Computer Science Principles Summer Work

Uploaded by

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

AP Computer Science Principles Summer Work

Uploaded by

lukecooper2027
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Describe the purpose of this program.

Who might use


it? What purpose does it serve?
a. This program asks the user to input a list of house prices. Once the
computer has these prices it asks the user whether the user wants to see
the median price of those houses, the mean price of the houses, the min
and max or to see all the houses that cost less or more than 1,000,000
dollars. This code is useful for anyone in the real estate agency, who
might want to track a list of houses in a town. For example in Westport,
every week or month they publish all the houses sold and the pieces they
sold for. You put that data through this code to find the mean, median and
min and max and then you could also find out how many houses were
under and over 1,000,000 dollars.
2. The program uses a Python data structure called a list
to store multiple values that are entered by the user.
Why is this list necessary in the program? Would you be
able to write the program without the list? If so, would
that limit the program’s capabilities or the way in which
the user could interact with it? Explain.
a. A list allows multiple values to be stored and compared, in this program
you need the list to store all the prices of the house. However, you could
do it without lists by defining all the different prices with a different
variable. For example you do price1 = input(“Please enter price 1”), price2
= input(“Please enter price 2”), ect. You could then find the mean, median,
min, max, and the number of houses under/over 1,000,000 dollars. Even
though this sort of works, it does not fully work because you could only
enter a certain number of house prices. While in the list you can enter 5,
10, 20 house prices or as many as you want to enter.
3. Algorithms and computer programs (simple ones like
this one) can also be represented using flow chart
diagrams. Below these questions there is an example of
a simple algorithm in Python and its associated flow
chart diagram so that you can see how the two are
related. Note that this simple program/algorithm is
completely unrelated to the main Housing Prices
program that you are analyzing. It’s simply provided
along with its flowchart version so that you can see how
program code and flowcharts are, in general, connected.
Choose a section of the Housing Prices program that
demonstrates all of the following
(a) selection
● Selection is when the computer chooses what will happen based on
information it is given so like an if or elif statement
● if choice == 1:
house_prices.sort()
n = len(house_prices)
if n % 2 == 0:
median = (house_prices[n // 2 - 1] +
house_prices[n // 2]) / 2
else:
median = house_prices[n // 2]
print(f"The median price is: {median}")
elif choice == 2:
total = 0
for price in house_prices:
total += price
mean = total / len(house_prices)
print(f"The mean price is: {mean}")
elif choice == 3:
min_price = house_prices[0]
max_price = house_prices[0]
for price in house_prices:
if price < min_price:
min_price = price
if price > max_price:
max_price = price
print(f"The minimum price is: {min_price}")
print(f"The maximum price is: {max_price}")
elif choice == 4:
under_million = 0
over_million = 0
for price in house_prices:
if price < 1000000:
under_million += 1
else:
over_million += 1
print(f"Number of houses under $1,000,000:
{under_million}")
print(f"Number of houses over $1,000,000:
{over_million}")
else:
print("Invalid choice.")
(b) sequencing
● Sequencing is a list of information
● # Initialize an empty list to store house prices
house_prices = []
(c) iteration
● Iteration is when something is repeated, so this part of the code is
repeated with a while statement:
● while True:
price = input("Enter the sale price: ")
if price.lower() == 'q':
break
else:
try:
house_prices.append(float(price))
except ValueError:
print("Invalid input. Please enter a
numerical value or 'q' to quit.")

4. Suppose that someone writes another function called

removeHousesUnderPrice(amount, list_of_values)

They want to allow the user of the program an option to


perform all four of the operations that it already
performs but only after excluding all houses below a
certain price. You can assume that the function works
as intended when provided with the appropriate
arguments. Explain how you could use this function in
the program to accomplish the desired effect. Where
would you call the function? What values would you
pass it?
● In the beginning I would ask the user to choose a number 1-5, 1-4 is the
same thing as it is in the original program while 5 is an option that allows
the user to give all the operations above a certain price. If they choose 5 I
would ask them to give me the amount and call a function called
removeHousesUnderPrice(amount_given, list_of_values). I would call it
right before these two lines:
else:
print("Invalid choice.")
In the actual removeHousesUnderPrice function I would use a for loops to
go through each value in the list_of_values, if the value in the list is above the
amount_given then it gets appended into another list called newList, if it is not
above the amount_given then nothing happens and the next value is evaluated.
Once the for loops are done I would then find the mean, median, min and max of
the newList, using the same way I used for each other previous choices(1-4).

You might also like