'In python I tried to find max value in a list without max() in a list but it shows "AttributeError: 'list' object has no attribute 'findmax' "
a=int(input('Enter number of values:'))
l=[]
for i in range(0,a):
b=input('*Enter the value:')
l.append(b)
print(l)
def findmax():
j=0
for i in range(0,a):
k=j+1
if(l[j]>l[k]):
print (l[j]," is the max value")
else:
j=j+1
l.findmax()
Solution 1:[1]
findmax is not an attribute of a list, it is a function. You have to call the function by writing findmax(), however a good practice would be to define your function for any list and not just for your list l. The program also has issues when you do if(l[j]>l[k]): because you are out of range.
Here is a piece of code that works as intended I believe.
def findmax(myList):
myMax = myList[0]
for i in myList:
if i > myMax:
myMax = i
return myMax
l = [1,2,3,4]
print(findmax(l))
This is worth a read : Difference between methods and attributes in python
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
