Same code differen results: simple lists in python

  Kiến thức lập trình

So we had a python beginners class, and all well and good, but in the exercise of the day, even if I copy my professors code to the letter, we get different results.

d={'ana':10, 'hermina':41, 'miha':12}
def najstarejša(d):
    najstarejši = None
    for oseba in d:
        if najstarejši == None or d[oseba] > d[najstarejši]:
            najstarejši = oseba
    return najstarejši
x=najstarejša(d)
print(x)

def najstarejša2(d):
    kandidat = None
    kandidatova_starost = None
    for (oseba,starost) in d.items():
            if kandidat == None or starost > kandidatova_starost:
            kandidat = oseba
            kandidatova_starost = starost
        return kandidat
y=najstarejša2(d)
print(y)

Both of these should give the the key with the highest value.
But for me the first works as intended, the second gives me the first key. Regardless of value, regardless of where I move who around. He is running it on macOS, i am doing it in Linux.

Kind regards.

LEAVE A COMMENT