Python Forum
Methods that return the highest score from the list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Methods that return the highest score from the list (/thread-17009.html)



Methods that return the highest score from the list - erfanakbari1 - Mar-24-2019

Hello guys !
I just got an exercise to do and I wrote some code for this task but unfortunately I'm facing some errors !
The task is :
# to write methods that return the highest score from the list, the last added score and the three highest 
# scores.
I solved the first ones ( return highest score and the last added score successfully . ) but I got stuck in last one which is to return the three highest scores
and this is my code and functions for doing it
def Three_highest_scores(a, b, c):
    for j in gamePlayersList:
        a = max(gamePlayersList)
        print(j)
        return a

        for f in gamePlayersList:
            if f < a:
                b = max(f)
                print(f)
                return b

                for x in gamePlayersList:
                    if a > x < f:
                        c = max(x)
                        print(x)
                        return c


Three_highest_scores(max(gamePlayersList), max(f), max(x))
print(Three_highest_scores())
And this is the error :
 Three_highest_scores(max(gamePlayersList), max(f), max(x))
NameError: name 'f' is not defined 
So , I'm very thankful if you can help me fix these issues .
Thanks


RE: Methods that return the highest score from the list - Larz60+ - Mar-24-2019

I get the following error (FYI: Please always post errors as below, in error tags, complete, unaltered)
Error:
Traceback (most recent call last): File ".../Projects/T-Z/T/TryStuff/src/Mar24-2019_post1.py", line 21, in <module> Three_highest_scores(max(gamePlayersList), max(f), max(x)) NameError: name 'gamePlayersList' is not defined
Which clearly states problem 1... 'No gamePlayersList'


RE: Methods that return the highest score from the list - erfanakbari1 - Mar-24-2019

(Mar-24-2019, 04:30 PM)Larz60+ Wrote: I get the following error (FYI: Please always post errors as below, in error tags, complete, unaltered)
Error:
Traceback (most recent call last): File ".../Projects/T-Z/T/TryStuff/src/Mar24-2019_post1.py", line 21, in <module> Three_highest_scores(max(gamePlayersList), max(f), max(x)) NameError: name 'gamePlayersList' is not defined
Which clearly states problem 1... 'No gamePlayersList'
Ok , fine .
So how can I fix this error and write correct program for this task ? ?


RE: Methods that return the highest score from the list - Larz60+ - Mar-24-2019

you will need to supply gamePlayersList
assuming it is indeed a list, somewhere early on in your script add:
gamePlayersList = ['value1', 'value2', ...]
replacing value1... with actual values


RE: Methods that return the highest score from the list - erfanakbari1 - Mar-24-2019

(Mar-24-2019, 06:47 PM)Larz60+ Wrote: you will need to supply gamePlayersList assuming it is indeed a list, somewhere early on in your script add:
 gamePlayersList = ['value1', 'value2', ...] 
replacing value1... with actual values
Thanks man
But now that I defined my list outside of function
I can not execute my program .
Just same as before
This is all of my code
gamePlayersList = [157, 298, 108, 2, 30, 12, 70]
print(gamePlayersList)


def frogger(highest, last):
    for i in gamePlayersList:
        highest = max(gamePlayersList)
        last = gamePlayersList[-1]
        print(highest)
        print(last)
        return highest
        return last


print(frogger(max(gamePlayersList), gamePlayersList[-1]))


def Three_highest_scores(a, b, c):
    for j in gamePlayersList:
        a = max(gamePlayersList)
        print(j)
        return a

        for f in gamePlayersList:
            if f < a:
                b = max(f)
                print(f)
                return b

                for x in gamePlayersList:
                    if a > x < f:
                        c = max(x)
                        print(x)
                        return c


Three_highest_scores(max(gamePlayersList), max(f), max(x))
print(Three_highest_scores())
Don't you think that there is something wrong with m y functions or definition of values ?


RE: Methods that return the highest score from the list - Larz60+ - Mar-24-2019

It's all one program. It's fine if not combined into a class, but the rules are a bit different.
Things have to defined before being used.

The gamePlayersList error is fixed, the f error is a new error.
since python is interpreted, you only see errors as they are encountered.
The error you are getting now is because you are trying to use f before it's defined:
hree_highest_scores(max(gamePlayersList), max(f), max(x))
you are mixed up on what goes where.

a, b, and c are attributes of the function Three_highest_scores. Inputs if you will, not outputs.

for outputs, you return values, which can be modified input values
you are trying to assign values to the input arguments of the function from within the function, that's now how it's done.
consider the following:

def ziggy(a, b):
    return a + b

print(ziggy(2, 4))
output:
Output:
6
a and b are input arguments, you must supply these values
the function adds a and b and returns the result


RE: Methods that return the highest score from the list - erfanakbari1 - Mar-25-2019

(Mar-24-2019, 11:20 PM)Larz60+ Wrote: It's all one program. It's fine if not combined into a class, but the rules are a bit different. Things have to defined before being used. The gamePlayersList error is fixed, the f error is a new error. since python is interpreted, you only see errors as they are encountered. The error you are getting now is because you are trying to use f before it's defined:
 hree_highest_scores(max(gamePlayersList), max(f), max(x)) 
you are mixed up on what goes where. a, b, and c are attributes of the function Three_highest_scores. Inputs if you will, not outputs. for outputs, you return values, which can be modified input values you are trying to assign values to the input arguments of the function from within the function, that's now how it's done. consider the following:
 def ziggy(a, b): return a + b print(ziggy(2, 4)) 
output:
Output:
6
a and b are input arguments, you must supply these values the function adds a and b and returns the result
Thanks man ! It really did solve my problem and finally worked Smile


RE: Methods that return the highest score from the list - aankrose - Mar-26-2019

I think you can make your code more simplified , please find the below code

gamePlayersList = [157, 298, 108, 400, 2, 30, 12, 70]
print(gamePlayersList)


def frogger():
    for i in gamePlayersList:
        highest = max(gamePlayersList)
        last = gamePlayersList[-1]
        print(highest)
        print(last)
        S=(sorted(gamePlayersList, reverse=True))
        print(S[0:3])
        break


d=frogger()
Output:
[157, 298, 108, 400, 2, 30, 12, 70] 400 70 [400, 298, 157] Process finished with exit code 0