Home > front end >  Returning tuple instead of integer
Returning tuple instead of integer

Time:12-27

def max_product(nums):
    product = 1, 
    maxP = (min(nums))
    print(type(maxP))

    for n in nums:
        print(type(n))
        product *= n # here, product should be an integer but it is a tuple, don't know why 
        print(product)
        print(type(product))
        maxP = max(product, maxP)  # here, it is giving this error: 'TypeError: '>' not supported between  

                                     instances of 'int' and 'tuple''
        if n == 0:
            product = 1
        
    product = 1
    for n in nums[::-1]:
        product *= n # here also it is tuple
        maxP = max(product, maxP)
        if n == 0:
            product = 1
    
    return maxP


print(max_product([2, 3, -2, 4]))

# desired output: 6

I am trying to write a function in python that returns the maximum product of continuous elements in an array containing both positive and negative numbers. But the function is not returning the desired result. It is giving the error mentioned in the code. Please help me to find the error and solve the problem.

the type of 'product' should be integer, but it is of type 'tuple', as a result of that the 'max()' function is not able to work and giving the error.

CodePudding user response:

I think that maybe you need to remove the comma , in the line: product = 1,, this makes the product variable of tuple type instead of int.

  • Related