1

I am trying to find maximum number from the list but not able to find the logical error in the code

def find_max(numbers):
    data = numbers.split(" ")
    maximum = data[0]
    for number in data:
        if number > maximum:
            maximum = number
        return maximum


number = input("Enter number with space:")
answer = find_max(number)
print(answer)
2
  • 1
    what data type do you think data[0] is? Commented Apr 4, 2019 at 17:49
  • I was expecting it to be int Commented Apr 4, 2019 at 18:00

4 Answers 4

3

The return statement is inside the for, so this will return always the first number entered. Also, you are comparing strings with int. Try this (is a solution of your own code, there are better ways to do this)

def find_max(numbers):
    data = numbers.split(" ")
    maximum = int(data[0])
    for number in data:
        if int(number) > maximum:
            maximum = int(number)
    return maximum


number = input("Enter number with space:")
answer = find_max(number)
print(answer)
Sign up to request clarification or add additional context in comments.

Comments

1

You can change your

data = numbers.split(" ")

to

data = map(int, numbers.split(" "))

to make it work. For Python 3 you need

data = list(map(int, numbers.split(" "))

Comments

0

You could make use of the max here. The reason why your code doesn't work is because you did not convert the numbers to integes, they are still strings in your script.

The classical way:

def find_max(numbers):

    # if the list is empty return nothing
    if len(numbers) <= 0:
        return None

    # iterate to find the maximum
    maximum = numbers[0]
    for number in numbers:
        if number > maximum:
            maximum = number

    return maximum


numbers = input("Enter numbers with space:")

# transform the numbers string into a list of strings
numbers = numbers.split(" ")

# prepare numbers by converting them into integers
integers = []
for i in range(len(numbers)):
    number_as_integer = int(numbers[i])
    integers.append(number_as_integer)

# find the max
answer = find_max(integers)
print(answer)

The enhanced way:

def find_max(numbers):
    return max(map(int, numbers.split(" ")))


number = input("Enter numbers with space:")
answer = find_max(number)
print(answer)

5 Comments

it might also help a new user to show them how it is done with a for-loop so that they can learn the logic behind it, as opposed to just throwing a map at them which might be confusing
@aws_apprentice it really depends on the objectives of the OP. But in general you are right, a novice should be able to compute a max without special functions, and and the same time he/she should also learn about these specials from Python.
@aws_apprentice I added also a more comprehensive / classical way
I convert the string to integer but still there is logical error. Can you point out? I added this line in the fucntion data1 = numbers.split(" ") data = list(map(int, data1))
@hurat your return maximum is indented too much, it's in the forloop, hence at the first iteration of the forloop it will return the first maximum, which in your case is data[0]
0

found the problem. There were two problems. 1) That I need to convert string into int 2) my return function should have been below the for loop. Here is the corrected code

def find_max(numbers):
    data1 = numbers.split(" ")
    data = list(map(int, data1))
    maximum = data[0]
    for number in data:
        if number > maximum:
            maximum = number
    return maximum


number = input("Enter number with space:")
answer = find_max(number)
print(answer)

1 Comment

Thank you guys.I appreciate it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.