Python Forum
Methods that return the highest score from the list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Methods that return the highest score from the list
#1
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
Reply
#2
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'
Reply
#3
(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 ? ?
Reply
#4
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
Reply
#5
(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 ?
Reply
#6
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
Reply
#7
(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
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,455 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,454 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Return the sum of the first n numbers in the list. pav1983 3 4,048 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  How to assign a "Score" variable to each element in a list Reta 5 5,303 May-16-2019, 01:13 PM
Last Post: ichabod801
  Return not vowels list erfanakbari1 2 2,679 Mar-26-2019, 11:37 AM
Last Post: perfringo
  List not passing between methods inside a class definition ijosefson 5 4,428 Oct-15-2017, 07:15 PM
Last Post: ijosefson
  how to get the highest monthly average temperature? nikhilkumar 2 7,015 Jul-25-2017, 02:33 PM
Last Post: DeaD_EyE
  Returning the highest value out of 3 values ComputerSkillet 2 3,843 Apr-28-2017, 03:16 AM
Last Post: volcano63
  Numerically determining the highest a ball travels when shot straight up JakeWitten 3 3,423 Apr-22-2017, 04:37 PM
Last Post: Ofnuts
  Average score MartinEvtimov 5 6,819 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020