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


Messages In This Thread
what function should i use to tidy up my code (extreme beginner) - by scraig0117 - Dec-16-2019, 02:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 851 May-19-2023, 11:19 AM
Last Post: deanhystad
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 2,017 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  Code not reading http link from .txt file (Beginner level) plarrip 3 2,454 Dec-17-2020, 11:33 PM
Last Post: bowlofred
  Beginner: I need help understanding few lines of a code. hop_090 1 1,717 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,810 Jun-18-2020, 04:59 PM
Last Post: QTPi
  A beginner code... TheDude 7 3,328 Jun-18-2020, 05:39 AM
Last Post: TheDude
  [Beginner] Code is not producing desired result fakej171 2 2,465 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,450 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Beginner trying to code in python RA0211 1 1,861 Sep-26-2019, 11:10 AM
Last Post: emryscass
  Beginner problem, replace function with for loop Motley_Cow 9 4,692 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