From Logic to Code: Printing Triangles#
In this assignment, you will get your first taste of converting an algorithm into code. In particular, you will write a function that prints out triangles and calculates their approximate area.
For example, if told to print a triangle of height 4, the function would print out:
Here is a triangle with height 4:
*
**
***
****
It would also count the number of stars in the triangle, which you can also think of as a crude approximation of the area of the triangle. In this triangle, for example, there are 10 total stars.
(While crude, this does actually provide a realistic sense of how computers solve many problems — you define a simple procedure to generate something you want to understand, then measure its properties. Similarly, properties like area are often measured by thinking of objects as being divided into discrete parts. Engineers call this “finite element analysis.”)
Setup the Assignment#
To begin, in VS Code create a new file and save it as triangle.py
(all lower case).
Then copy the following scaffolding into that file:
def print_triangle(size):
# Start with star_count being 0.
# Count from 0 (inclusive) to size (exclusive),
# and for each number i that you count,
# count from 0 (inclusive) to i (inclusive),
# and for each number j that you count,
# print a "*" without printing a newline,
# (pass the optional parameter end='' to print)
# increment star_count.
# When you finish counting on j,
# print a newline
# (remember Python adds a newline, so really print "")
# When you finish counting on i,
# your answer is star_count.
pass
def main():
print("Here is a triangle with height 4:")
num_stars = print_triangle(4)
print("That triangle had " + str(num_stars) + " total stars.")
# Now print "Here is a triangle with height 7:"
# Then call print_triangle, passing in 7,
# and assign the result to num_stars
# Finally, print "That triangle had [num_stars] total stars." such
# that the [num_stars] prints the value of num_stars.
main()
Writing Your Code#
Your job is to convert this algorithm (written in English!) into Python code.
Note that one of the lines of the algorithm calls for you to print a "*"
without a newline. When you call print
in Python, it adds a newline by default. To not get a newline added, you need to pass an optional named parameter end=""
to print, like this:
print("*", end="")
In main, there are three print statements that print out a triangle with height 4 and the total number of stars in that triangle (returned by the print_triangle
function).
There is also a comment asking you to write a few more statements to do the same thing for a triangle of height 7.
Add the code to do this.
Checking Your Answers#
When completed, your code should print out the following:
Here is a triangle with height 4:
*
**
***
****
That triangle had 10 total stars.
Here is a triangle with height 7:
*
**
***
****
*****
******
*******
That triangle had 28 total stars.
Submitting on Gradescope#
Once you feel your code is operating correctly, open the Gradescope page for this class in your browser and navigate to the “Exercise: triangle.py” assignment. Then upload your triangle.py
file.
Note that the autograder will not just check these two examples — it will also test several additional number pairs to ensure your function is working properly.
The autograder will take a couple minutes to run. Once complete, you should see a few things.
If the name of your file is not what the autograder expected (for this assignment,
max.py
), you will get an error saying as much.If your code is not properly formatted using
black
(reminder for how to setup format on save here), you will get an error saying that.If the autograder is unable to find a
max
function in your file, you will also get an error.