Home > Net >  Define stubs in python
Define stubs in python

Time:09-30

I am stuck on this homework problem. This is the assignment question and prompt:


"Define stubs for the functions get_user_num() and compute_avg(). Each stub should print "FIXME: Finish function_name()" followed by a newline, and should return -1. Each stub must also contain the function's parameters.

Sample output with two calls to get_user_num() and one call to compute_avg():

FIXME: Finish get_user_num()
FIXME: Finish get_user_num()
FIXME: Finish compute_avg()
Avg: -1"

I have tried a bunch of different things, but I just keep getting errors. I'm completely lost on this one. This is what I have so far:


def get_user_num(user_num1, user_num2):
    get_user_num = -1
    avg = user_num1   user_num2
    return get_user_num

user_num1 = 0
user_num2 = 0
avg_result = 0

user_num1 = get_user_num()
user_num2 = get_user_num()
avg_result = compute_avg(user_num1, user_num2)

print('Avg:', avg_result)

And I am just getting error message after error message. I know I have to use def to define something, but I'm lost.

CodePudding user response:

So let's start simple, you got some statement, let's parse it into simple parts and connect it into the Python world.

  • Define stubs

    noun Something cut short or arrested in development. (source)

  • for the functions get_user_num() and compute_avg()

    The keyword def introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented. (source)

  • Each stub should print

    Check how to output something with Python to the console.

  • followed by a newline

    As in do not make two FIXME lines collapse onto each other like this: FIXME: somethingFIXME: something else.

    the Unix end-of-line convention '\n', the Windows convention '\r\n', and the old Macintosh convention '\r'... (source)

  • and should return -1

  • Each stub must also contain the function's parameters.

    Undefined anywhere. So either use something you'd normally use for a let's say maths function or likes

  • Sample output with two calls to get_user_num() and one call to compute_avg(): ...

    This is how the final program should behave once executed. A function is called by first being ensuring it's been defined previously, then writing its name () e.g. func().

    Judging by the undefined func arguments and two related functions, you might want to connect the two functions either directly or with an intermediary item holding the result.

After these steps you should be able to create the final program. Do not try to create it at once, utilize the bullet points as small steps and always check if the file still works (executes properly). Create multiple files (copy-paste) when necessary.

Nobody cares how it looks like while developing. You're learning, you'll become better eventually.

CodePudding user response:

I found the solution!!! Thank you all for the help. It really did just take trial and error and making sense of the error messages.

'''

def get_user_num():
    print('FIXME: Finish get_user_num()')
    return -1
    
def compute_avg(user_num1, user_num2):
    print('FIXME: Finish compute_avg()')
    return -1


user_num1 = 0
user_num2 = 0
avg_result = 0

user_num1 = get_user_num()
user_num2 = get_user_num()
avg_result = compute_avg(user_num1, user_num2)

print('Avg:', avg_result)

'''

  • Related