0

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 because I don't have anyone in my life that's experienced in this subject and that can help me.

  1. this one uses a function to determine if any year is a leap year. Make your function return a boolean value. (Leap years are divisible by 4 except if they occur at the beginning of a new century that is not divisible by 400. 1900 was not a leap year; 2000 was.)
  2. a program that accepts two points on a line and returns the values for m and b in the equation y = mx + b. (m = (y1 - y2) / (x1 - x2) and b = y1 - mx1)
  3. a program that uses the Euclidean algorithm in a subprogram to find the greatest common factor of two numbers. Example: The GCF of 45 and 55 is 5, from the sequence: 55, 45, 10, 5, 0 . Return the GCF

coding for 1.

def isLeapYear(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

coding for 2.

    rise = y2-y1
    run = x2-x1
    m = rise/run
    b = y2/(m*x2)
    return print("m = " + str(m) + " and b = " + str(b))

coding for 3.

def gcf(n1,n2):

    remainder = None

    while remainder != 0:
        remainder = n1 % n2
        n1 = n2
        n2 = remainder


    return n1

EDIT: sorry im bad at explaining things, like for example, i would want the leapyear one to function like; "Enter your leap year!" (stores function) sorry, but (leapyeartheyentered) is not a leap year! (or) (leapyear they entered) is a leap year!

yes someone phrased it because i'm bad at english too i suppose- i want to print the results of the coding

IJH
  • 107
  • 1
  • 7

2 Answers2

2

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 user to enter a year, convert to int

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:

def calcLine(x1, y1, x2, y2):
    rise = y2-y1
    run = x2-x1
    m = rise/run
    b = y2 - (m*x2) # math fix, as suggested in the comments above
    return 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 calculation

print("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!

Blckknght
  • 100,903
  • 11
  • 120
  • 169
1

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:

return print("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)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
iliar
  • 932
  • 6
  • 11
  • Have you checked before posting? – gboffi Nov 12 '19 at 21:59
  • Maybe input(...) always returns a string? – JohanC Nov 12 '19 at 22:02
  • 1
    The type check of the `input` return value only works in Python 2, which I doubt a new programmer is going to be using these days (since official support is finally going to end at the end of the year). In Python 3, `input` always returns a string, as JohanC says. – Blckknght Nov 12 '19 at 22:05
  • Yep you're right, I tested it on python2. Fixed it. I don't think a try except is appropriate for op's current knowledge of the language. – iliar Nov 12 '19 at 22:18