I have been trying to find the maximum element in a list using a simple program
print ("Enter the elements of array seperated by spaces : ")
t = str(raw_input())
arr = list()
arr = map(long,t.split(' '))
print arr
print max(arr)
for i in arr:
m=arr[0]
if i>m:
m=i
print ("The largest element in arary is : "+str(m))
The input that I passed in was :
1 2 3 4 5 23682967 4574 433
And the expected answer for the same should be 23682967
which is what I'm able to get using the inbuilt function of python called max()
but not from the code that I've written,
The output that my code gives is : 433
Can someone please guide me in understanding this behavior of python ?
m=arr[0]should be outside theforloopraw_inputalready returns a string, so no need to usestron it. (2)raw_inputsupports giving a prompt, so no need to useprintbefore. (3) The first assignment ofarrdoes nothing, because you reassign it in the next statement. You can just remove that line.