Python Forum
what function should i use to tidy up my code (extreme beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what function should i use to tidy up my code (extreme beginner)
#1
class Players:
    def __init__(self, full_name, goals, shots, headers, tackles, passes, clean_sheets):
        self.nm = full_name
        self.gl = goals
        self.st = shots
        self.hd = headers
        self.tk = tackles
        self.ps = passes
        self.cs = clean_sheets


morelos = Players('Alfredo Morelos', 35, 10, 5, 5, 6, 10)
kent = Players('Ryan Kent', 20, 10, 7, 5, 6, 8)
defoe = Players('Jermaine Defoe', 30, 6, 5, 5, 6, 8)
tav = Players('James Tavernier', 4, 6, 7, 7, 5, 6)
mcgregor = Players('Allan McGregor', 20, 5, 5, 6, 7, 6)
barisis = Players('Borno Barisic', 10, 30, 3, 5, 6, 10)
goldson = Players('Conor Goldson', 20, 10, 7, 5, 6, 8)
katic = Players('Nikola Katic', 30, 6, 5, 5, 6, 8)
jack = Players('Ryan Jack', 4, 6, 7, 7, 5, 6)
davis = Players('Steve Davis', 20, 5, 5, 6, 7, 6)


def statistic_grab():
    if player_name == 'am' and player_stat == 'goals':
        print(morelos.nm, 'has scored', morelos.gl, 'goals')
    elif player_name == 'am' and player_stat == 'shots':
        print(morelos.nm, 'has taken', morelos.st, 'shots')
    elif player_name == 'am' and player_stat == 'headers':
        print(morelos.nm, 'has made', morelos.hd, 'headers')
    elif player_name == 'rk' and player_stat == 'goals':
        print(kent.nm, 'has scored', kent.st, 'goals')
    elif player_name == 'rk' and player_stat == 'headers':
        print(kent.nm, 'has made', kent.hd, 'headers')
    elif player_name == 'rkdefoe' and player_stat == 'shots':
        print(kent.nm, 'has taken', kent.st, 'shots')
    elif player_name == 'jd' and player_stat == 'headers':
        print(defoe.nm, 'has made', defoe.hd, 'headers')
    elif player_name == 'jd' and player_stat == 'goals':
        print(defoe.nm, 'has scored', defoe.st, 'goals')
    elif player_name == 'jdtav' and player_stat == 'headers':
        print(defoe.nm, 'has made', defoe.hd, 'headers')
    elif player_name == 'jt' and player_stat == 'goals':
        print(tav.nm, 'has scored', tav.st, 'goals')
    elif player_name == 'jt' and player_stat == 'headers':
        print(tav.nm, 'has made', tav.hd, 'headers')
    elif player_name == 'jt' and player_stat == 'shots':
        print(tav.nm, 'has taken', tav.st, 'shots')
    else:
        print("Invalid input")


print('Welcome to Rangers Player Statistics\n')
new_stat = input('Would you like to choose a stat?     \n')
while new_stat == 'yes':
    print('The players we have statistics for are:\nAlfredo Morelos\nRyan Kent\nJermaine Defoe\nJames Tavernier\n'
           'Allan McGregor\nBorno Barisic\nConor Goldson\nNikola Katic\nRyan Jack\nSteve Davis\n')
    player_name = input('Which player would you like statistics on?  (please enter player initials)   \n')
    player_stat = input('Which statistic would you like?\nGoals\nShots\n Headers     \n')
    statistic_grab()
    new_stat = input('Would you like another stat?     \n')
    if new_stat == 'no':
        print('Thank you, Goodbye')
        break
    elif new_stat == 'yes':
        continue
    else:
        new_stat = input('Invalid input, please enter yes or no:     \n')
        if new_stat == 'no':
            print('Thank you, Goodbye')
            break
        elif new_stat == 'yes':
            continue
My question is about the statistic_grab section, the whole code has complete functionality at the minute
but I'm unsure what function or method I should use to make my code efficient. As you can see, only some
of the data needed is in the code at present and its already getting ridiculously repetitive. All I am after is
guidance on what to use then I want to research how to use it and implement myself. Thank you in advance :)
Reply
#2
that is example that you should keep information out if your variable names.
In this case you need to create a data structure, e.g. dict to hold your players.
Note that class name is better to be singular Player, not Players, i.e. it represent a single player
There are many other things that can be improved, but that is to answer your specific question
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Buran, thanks a lot for your help, and apologies for the rookie mistake. Are you meaning where the class is it should be a dictionary, I had tried to use that before I learnt a little more on classes. Since a dictionary can only have a key and a value, should I be looking to use nested dict maybe or lists within dict?

Sorry again, possibly trying to run before I can walk, but believe having a project from the get go that is personal to my interests will help me enjoy to learning side of coding more and keep motivation intact.
Reply
#4
the keys of the dicts will be the player names or their nicknames,e .g. am (depending how you expect your user to provide the search term).
the values will be instances of class Player, e.g. with just first three players
class Player:
    def __init__(self, full_name, goals, shots, headers, tackles, passes, clean_sheets):
        self.nm = full_name
        self.gl = goals
        self.st = shots
        self.hd = headers
        self.tk = tackles
        self.ps = passes
        self.cs = clean_sheets
 
 
players = {'am':Player('Alfredo Morelos', 35, 10, 5, 5, 6, 10),
		   'rk':Player('Ryan Kent', 20, 10, 7, 5, 6, 8),
		   'jd':Player('Jermaine Defoe', 30, 6, 5, 5, 6, 8)}
then you can access them as with normal dict, using the key
You should know that everything in python is object. it doesn't matter if it is some built-in type or custom class - it can be value in a dict

If you don't plan to expand the class, built-in namedtuple from collections module would do just the same job.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
perfect explanation man, I think I've had the blinkers on and wasn't realising just how dynamic the various problem solving options can be used, if I'm right now I will calling for all the data the user asks for very similar to my loop but ill be able to condense it so to speak and display the information using a formula rather than literally having to use wording in a 'sentence'?

Thanks again, makes it easier to learn this stuff yourself when there are people out there willing to answer the most basic stuff.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 821 May-19-2023, 11:19 AM
Last Post: deanhystad
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,998 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  Code not reading http link from .txt file (Beginner level) plarrip 3 2,419 Dec-17-2020, 11:33 PM
Last Post: bowlofred
  Beginner: I need help understanding few lines of a code. hop_090 1 1,691 Sep-07-2020, 04:02 PM
Last Post: Larz60+
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,746 Jun-18-2020, 04:59 PM
Last Post: QTPi
  A beginner code... TheDude 7 3,291 Jun-18-2020, 05:39 AM
Last Post: TheDude
  [Beginner] Code is not producing desired result fakej171 2 2,436 Mar-21-2020, 10:26 AM
Last Post: buran
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 2,421 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Beginner trying to code in python RA0211 1 1,846 Sep-26-2019, 11:10 AM
Last Post: emryscass
  Beginner problem, replace function with for loop Motley_Cow 9 4,630 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow

Forum Jump:

User Panel Messages

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