Writing Your First Function (max)

Writing Your First Function (max)#

In this exercise, you will write your first simple function in Python and submit it on gradescope. The goal of this exercises is mostly to get you familiar with the process of writing, testing, submitting, and revising code in this class.

Setup the Assignment#

To begin, in VS Code create a new file and save it as max.py (all lower case). Each assignment will have a different name, and it’s important that you give the file for each assignment the correct name to help the autograder identify your submission.

Then copy the following text into the file. This “starter code” is meant to act as a scaffolding to help you with the assignment:

[Scaffolding begins below here]

def max(num1, num2):
    # check if num1 is greater than num2

    # if so, your answer is num1

    # otherwise, your answer is num2

    pass


def main():
    print("max(42, -69) is " + str(max(42, -69)))
    print("max(33, 0) is " + str(max(33, 0)))
    print("max(0.123, 0.223) is " + str(max(0.123, 0.223)))
    # print the max of -0.123 and -0.223

    return 0


main()

[Scaffolding ends above here]

Write Your Code#

As you can see, this scaffolding defines to functions: max and main.

In max, the algorithm is written as comments, but there is no code. You should translate this algorithm to code.

In main, there are three print statements which call max and print its result. These would let you check if max is working right.

There is also a comment asking you to write one more print statement with a call to max. In that comment, it asks you to take the max of two numbers and print them out. Add a line of code to do this.

When you have done this, first run your code with the “Run code” button in the upper right corner of VS Code. If you have any syntax errors, you will get information about where they are, and should fix them.

The print statement that are called when main runs are a set of example tests designed to give you quick feedback on whether your code is doing what it should do. The output expected from the four print statements (that is, the three already present plus the one you have been asked to add) is:

max(42, -69) is 42
max(33, 0) is 33
max(0.123, 0.223) is 0.223
max(-0.123, -0.223) is -0.123

Submit Your Assignment#

Once you feel your code is operating correctly, open the Gradescope page for this class in your browser and navigate to the “Exercise: max.py” assignment. Then upload your max.py file.

Note that the autograder will not just check these four 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.