Search code examples
pythonpython-3.xpython-3.2

how to use variebles in list[variable]


import sys
sort = []   #list
def myMax(mylist):   # function instead of max()
    listadd = 0
    plus1 = listadd + 1
    for listadd in range(a):
    if sort[listadd] > sort[plus1, a]: # where i get error
        return sort[listadd]
while True:
try:
    a = int(input(" How many numbers do you want to compare: "))
    break
except ValueError:
    sys.stderr.write('ERROR\n')
    sys.stderr.write(' Try Again... \n')
for i in range (a):
    while True:
        try:
            n = int(input("Please enter a number: "))
            sort.append(n)
            break
        except ValueError:
            sys.stderr.write('ERROR\n')
            sys.stderr.write(' Try Again... \n')
print(myMax(sort), " is the biggest number! ") # function instead of max()
SystemExit()

Simple max program error.

Program to find max. of some numbers without using max() and install required modules.

How do you make a function that does the same thing?


Solution

  • You need to loop over each item in the list, checking if it is larger that the highest one so far.

    x=lst[0]
    for i in lst:
        if i > x:
            x = i
    return x