2

Here's the prompt:

Assign max_sum with the greater of num_a and num_b, PLUS the greater of num_y and num_z. Use just one statement. Hint: Call find_max() twice in an expression.

Here's my code:

    def find_max(num_1, num_2):
   max_val = 0.0

   if (num_1 > num_2):  # if num1 is greater than num2,
      max_val = num_1   # then num1 is the maxVal.
   else:                # Otherwise,
      max_val = num_2   # num2 is the maxVal
   return max_val

max_sum = 0.0

num_a = float(input())
num_b = float(input())
num_y = float(input())
num_z = float(input())

def find_max(num_a, num_b):
    max_val = 0.0
    
    if (num_a > num_b):
        max_val = num_a
    else: 
        max_val = num_b

def find_max(num_y, num_z):
    max_val = 0.0
        
    if (num_y > num_z):
        max_val = num_y
    else:
        max_val = num_z
            
        

print('max_sum is:', max_sum)

Not sure what I've done wrong. I've tried this also


def find_max(num_a, num_b):
    max_1 = 0.0
    
    if (num_a > num_b):
        max_1 = num_a
    else: 
        max_1 = num_b

def find_max(num_y, num_z):
    max_2 = 0.0
        
    if (num_y > num_z):
        max_2 = num_y
    else:
        max_2 = num_z
            
max_sum = (max_1 + max_2)

That gives me an error message that "max_1 is not defined" even though I've defined it. The first code just gives me an output of 0.0.

Any tips? Thanks!

  • The prompt is suggesting that you **call** `find_max` twice, not that you **define** it twice more. You'd do this with the built-in `max` function like: `max_sum = max(num_a, num_b) + max(num_y, num_z)` – Samwise May 25 '21 at 01:44
  • if that wasn't clear -- delete the two extra `def find_max` functions. :) The idea of a function is that it accepts whatever parameters you give it, and uses those values within the function. With your single `def find_max(num_1, num_2)`, try printing the output of `find_max(1, 2)` and `find_max(20, 10)`. That one function can take **any** two numbers you give it, including numbers that are variables (like say `num_a` and `num_b`). – Samwise May 25 '21 at 01:55

3 Answers3

2
def find_max(num_1, num_2):

    max_val = 0.0
    if (num_1 > num_2):  # if num1 is greater than num2,
        max_val = num_1  # then num1 is the maxVal.
    else:  # Otherwise,
        max_val = num_2  # num2 is the maxVal
    return max_val

num_a = float(input())
num_b = float(input())
num_y = float(input())
num_z = float(input())

max1 = find_max(num_a, num_b)
max2 = find_max(num_y, num_z)

max_sum = max1 + max2

print('max_sum is:', max_sum)

Delete the extra two find_max()functions and just call it two times for max1 and max2.

Output:

20
30
13
56
max_sum is: 86.0
Rishabh Semwal
  • 328
  • 1
  • 3
  • 12
2

I don't understand why two times you are defining find_max function and you did not even call the function find_max() and it is better to use python's built in max function and you can also use f-strings

Just use:

num_input = 4
num_a, num_b, num_y, num_z = [float(input()) for _ in range(num_input)]
max_sum = max(num_a, num_b) + max(num_y, num_z)
print(f"max_sum is: {max_sum}")
Arkodeep Ray
  • 174
  • 14
0
def find_max(num_1, num_2):
max_val = 0.0

if (num_1 > num_2):  
  max_val = num_1  
else:               
  max_val = num_2   
return max_val

max_sum = 0.0

num_a = float(input())
num_b = float(input())
num_y = float(input())
num_z = float(input())

max_sum = find_max(num_a, num_b) + find_max(num_y, num_z)

print(f'max_sum is: {max_sum}')

This worked for me to get it into only one line of code. The prompt was pretty confusing because I thought it was asking for me to use relational operators.