0

I am writting a code to compare mathematic functions and say which one of them has the biggest value for given x and y

import math

def r(x,y):
  r = 3 * math.pow(x, 2) + math.pow(y, 2)
  return r

def b(x,y):
  b = 2 * math.pow(x, 2) + 5 * math.pow(y, 2)
  return b

def c(x,y):
  c = -100 * x + math.pow(y, 3)
  return c

def compare():
  x = float(input("Type the value of x: "))
  y = float(input("Type the value of y: "))
  r(x,y)
  b(x,y)
  c(x,y)

  if r > b and c:
    print("r is the biggest.")
    return
  elif b > r and c:
    print("b is the biggest.")
    return
  elif c > b and r:
    print("c is the biggest.")
    return

compare()

But I get the following error:

TypeError: '>' not supported between instances of 'function' and 'function'

0

2 Answers 2

1

You must assign the result of each function to a variable. Try the following:

import math

def r(x,y):
  r = 3 * math.pow(x, 2) + math.pow(y, 2)
  return r

def b(x,y):
  b = 2 * math.pow(x, 2) + 5 * math.pow(y, 2)
  return b

def c(x,y):
  c = -100 * x + math.pow(y, 3)
  return c

def compare():
  x = float(input("Type the value of x: "))
  y = float(input("Type the value of y: "))
  R=r(x,y)
  B=b(x,y)
  C=c(x,y)

  if R > B and R>C:
    print("r is the biggest.")
    return
  elif B > R and B>C:
    print("b is the biggest.")
    return
  elif C > B and C>R:
    print("c is the biggest.")
    return

compare()
Sign up to request clarification or add additional context in comments.

1 Comment

Your code carries over a bug from OP involving comparisons and and. ;)
0

There are two bugs here:

  • You need to compare the actual results of the function, not the function object itself.
  • You can't compare one numerical value to the and of two other values; what you're actually testing for in that case is a single comparison plus whether the other value is non-zero!

I'd use the max function to get the largest value rather than building a complicated chain of conditionals to do the same thing. That way if you add another function it's easy to just add it to the list:

import math

def r(x, y):
    return 3 * math.pow(x, 2) + math.pow(y, 2)

def b(x, y):
    return 2 * math.pow(x, 2) + 5 * math.pow(y, 2)

def c(x, y):
    return -100 * x + math.pow(y, 3)

def compare():
    functions = [r, b, c]  # maybe this could be a parameter?
    x = float(input("Type the value of x: "))
    y = float(input("Type the value of y: "))

    result, func_name = max((f(x, y), f.__name__) for f in functions)
    print(f"{func_name} is the biggest.")

compare()
Type the value of x: 1
Type the value of y: 2
b is the biggest.

Type the value of x: 0
Type the value of y: 0
r is the biggest.

Type the value of x: -1
Type the value of y: -2
c is the biggest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.