简体   繁体   中英

python won't count two digit number as highest value

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.

You should convert the scores to integers, eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM