3

List = [name, lines.split(":")[1]] Latest_scores = (lines.split(":")[1][-7:]) Highscore = max(latest_scores) Print(highscore)

Out of: 9,10,2, It says the highestvalue is 9, not 10 as if it is ignoring the two digit number.

Max Vapton
  • 29
  • 5
  • 9
    Convert them to integers before comparing: `max(latest_scores, key=int)`. – Ashwini Chaudhary Jul 28 '15 at 15:59
  • 7
    You might want to add an actual code snippet that contains everything needed to run, and demonstrates your problem. However, based on the fact that you are using `lines`, I will assume that you read it from a file, in which case the values are of type `str` and not of type `int`. – Bas Jansen Jul 28 '15 at 15:59
  • 1
    related : http://stackoverflow.com/questions/4806911/string-comparison-technique-used-by-python – Rod Jul 28 '15 at 16:02
  • 1
    It is comparing strings, not integers. As suggested before, convert them to integer. Use `nums = [int(num) for num in Latest_scores]` – Imanol Luengo Jul 28 '15 at 16:03

1 Answers1

8

You should convert the scores to integers, e.g. with map(int, Latest_scores). Strings are compared in alphabetic order, where 9 comes last and therefore is the maximum.

EDIT: From your comments it seems that Latest_scores is just a string. That would mean you're trying to find a maximum in a string. The max function then returns the "highest" character in the string. To make this work correctly, you have to split the string by the , character:

Latest_scores = lines.split(":")[1][-7:].split(",")
Highscore = max(map(int, Latest_scores))
Print(Highscore)
Teyras
  • 1,262
  • 11
  • 22