Reputation: 35
My problem is that I've got a for loop checking through a string and I want to find the max number in that string but for some reason it sometimes changes the max_ouput to a smaller number. for example if max_output = 254607 and the number its checking is 92186 it will make 92186 the new max_output even though it it not greater. Code I'm using:
def max_power(data):
max_output = 0
lines = data.split('\n')
for line in lines:
digits=line.split(',')[-9]
if digits > max_output:
max_output = digits
print max_output
This is the data set I'm working with: http://pastebin.com/1UpzeAgD
Upvotes: 1
Views: 95
Reputation: 27486
It looks like you are comparing string "254607" with string "92186" string comaprision works character by character and will stop as soon as it decides "9" > "2".
Try
if int(digits) > int(max_output):
Just needed to int() both
Upvotes: 1