Skip to content Skip to sidebar Skip to footer

How Do I Print The Results Of My Math Of My Functions? [python Functions] (edited)

I have 3 different small programs that I have no idea how to display them, I've done the math and stuff, I don't really know what else to say, other than I need desperate help beca

Solution 1:

It sounds like you're solving a homework problem, and the problem asks you to write functions to do three different things. It doesn't ask you to write wrapper code around them to call the functions and print out the results, but you may want that for your own reasons (e.g. to test the functions before handing in the assignment, or just to learn how).

Anyway, the key thing to learn is how to call the functions. For the leap-year function you probably want to make the call in an if statement, since you will print two different messages depending on the Boolean result you get returned.

year=int(input("Enter your leap year!")) # prompt the userto enter a year, converttoint

if isLeapYear(year):
    print(year, "is a leap year!")
else:
    print("I'm sorry,", year, "is not a leap year.")

For your second function, you probably need to change its code to return the m and b values, and leave the printing to the calling code, since that's what the problem statement says to do. In Python you can return a tuple of values, which for most purposes works like returning two values at once (you can pack and unpack tuples very easily). You cut off the name of your second function, but if we call it calcLine, the updated function and calling code could look like this:

defcalcLine(x1, y1, x2, y2):
    rise = y2-y1
    run = x2-x1
    m = rise/run
    b = y2 - (m*x2) # math fix, as suggested in the comments abovereturn m, b  # return a 2-tuple, rather than printing here

coords_string = input("Enter x1, y1, x2, y2 coordinates: ") # get a string of coordinates
x1, y1, x2, y2 = map(float, coords_string.split(','))       # parse the string into numbers

slope, intercept = calcLine(x1, y1, x2, y2)                 # do the calculationprint("m = {} and b = {}".format(slope, intercept))         # print our results

Don't worry if you don't fully understand the string parsing and formatting parts of the calling code, the key line for you to understand is the function call: m, b = calcLine(x1, y1, x2, y2). This calls the function that was defined up above, and saves the m and b values that were returned in a tuple and unpacks them into two new global variables that we can use later (I chose to use different names than m and b to make it clear that they're separate from the function's variable, thought they have the same values).

The last function is probably the easiest to deal with, though since you haven't said what you'd want to print out for it, I'll leave it to you!

Solution 2:

For the first question use:

y = int(input('Enter year: '))
if isLeapYear(y):
  print('%d is a leap year' % y)
else:
  print('%d is not a leap year' % y)

In the second question:

returnprint("m = " + str(m) + " and b = " + str(b))

returns None. That's because the function print returns None. It's used for printing.

You probably either wanted to create a function that returns something, or you wanted to print something.

If you need to return two things from a function you do:

return m,b

And then in the code that called that function:

m,b = CalcMB(x1,y1,x2,y2)

Post a Comment for "How Do I Print The Results Of My Math Of My Functions? [python Functions] (edited)"