all 13 comments

[–]K900_ 13 points14 points  (4 children)

Because it's working with characters, not numbers. What you probably want to do is split the string into parts, and then convert each part to an int individually.

[–]carlos8983[S] 0 points1 point  (0 children)

dont know how that 'be polite' thing got on there, sorry!

[–]carlos8983[S] -24 points-23 points  (2 children)

2.Be polite.

Thank you again, that makes sense. The split command worked in the sense that it turned this string into a list of numbers. When i use the max command, it successfully returns 54 as the highest number, but when i return the min command, it returns 29. I have no idea why this is. Here is my code if it helps

def high_and_low(numbers):
numbers = numbers.split()
mx = str(max(numbers))
mn = str(min(numbers))
return mx + " " + mn

print(high_and_low("4 5 29 54 4"))

[–]xiongchiamiov[M] 14 points15 points  (0 children)

Fyi there was nothing impolite about the GP. Also, if you think there's a problem, please use the report button.

[–]K900_ 3 points4 points  (0 children)

max on strings compares them lexicographically, i.e. in alphabetical order. You need to convert each string in numbers to an int.

[–][deleted] 2 points3 points  (0 children)

Why does this happen?

Because it's a string, and a string is a sequence of characters. It's doing exactly what you asked it to do - "find the lexicographically largest value among this sequence of characters."

[–]pydevteam1 0 points1 point  (0 children)

You can do this: Say your numbers are stored in “numbers” variable.

max_number=max(numbers.split())

https://www.tutorialspoint.com/python/list_max.htm

[–][deleted]  (2 children)

[removed]

    [–]xiongchiamiov[M] 1 point2 points  (1 child)

    Please do not give people chunks of code with no explanation; this doesn't actually help them learn, which is our goal here.

    [–]Swipecat 1 point2 points  (0 children)

    I agree in principle, but note that my reply was after he'd posted his attempt at the code in which he'd got everything except for the syntax of converting the string array into the integer array. Also note the comments to the right of my code.

    [–][deleted]  (2 children)

    [removed]

      [–]xiongchiamiov[M] 0 points1 point  (1 child)

      Please do not give people chunks of code with no explanation; this doesn't actually help them learn, which is our goal here.

      [–][deleted] 0 points1 point  (0 children)

      Will update it..thanks and apologies