#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Help with returning variables


    What i want to do is return the variables from the methods and then use them in their own print statement. I am new to this language, taking the class atm, but i built this to get more practice in.
    ////////////////////////////////////////////////////////
    # def main is the main function, then you put in the first function hockey
    # at the end when you are done put in main()
    def main ():
    fd = int(input("Are you a foward or goalie? , press 1 for foward, 2 for goalie, "))
    if fd == 1:
    hockey()
    else:
    goalie()
    output()

    def hockey(): # function body this is the temp for fowards and goalies
    b = 3
    goals = int(input ("please enter number of goals scored: "))
    assists = int(input("please enter number of assists: "))
    print ("you had",goals,"goals")
    print ("and",assists,"assists")
    points = goals + assists
    print ("totaling",(points),"points")
    if points >= b:
    print ("good game")
    else:
    print ("pick it up")


    def goalie(): # this is the app for goalies, need to fix percentage display
    # need to move the print functions to their own function
    # key : TM = total minutes SA = Shots Against Ga = Goals Against GAA = Goals Against average
    # key : SavesP = saves percentage
    tm = 45
    sa = int(input ("please enter number of shots against: "))
    # goals against
    ga = int(input ("please enter number of goals aginst: "))
    mins = int(input ("please enter number of minutes played: "))
    gaa = (ga * tm) / mins
    saves = sa - ga
    saveP = saves / sa

    print ("you had ",sa," shoats against")
    print ("you made ",(format (saves, '.3f')), " saves")
    print (" you played ",mins, " minutes")
    print ("this is your Goals Against Average ",gaa,)
    print ("this is your Save Percentage",saveP,)


    main()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Please improve this horrid response.
    Code:
    # Someone versed in design patterns please improve
    
    class Morph:
    
        def __init__(self,position):
            self.position = position
    
        def __str__(self):
            if self.position == 'goalie':
                return self.strGoalie()
            else:
                return self.strForward()
    
        def __call__(self):
            if self.position == 'goalie':
                return self.callGoalie()
            else:
                return self.callForward()
    
        def strForward(self):
            return NotImplemented
    
        def callForward(self):
            return NotImplemented
    
        def callGoalie(self):
            self.tm = 45
            self.sa = int(input ("please enter number of shots against: "))
            self.ga = int(input ("please enter number of goals aginst: "))
            self.mins = int(input ("please enter number of minutes played: "))
    
        def strGoalie(self):
            gaa = (self.ga * self.tm) / self.mins
            saves = self.sa - self.ga
            saveP = round(100*saves / self.sa)
            result = ['you had {} shoats against'.format(self.sa)]
            result.append("you made "+(format (saves, '.3f'))+ " saves")
            result.append("you played {} minutes".format(self.mins))
            result.append("this is your Goals Against Average {}".format(gaa))
            result.append("this is your Save Percentage {}%".format(int(saveP)))
            return '\n'.join(result)
    
    def main():
        player = Morph('goalie')
        player()
        print(str(player))
    
    main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    492
    Rep Power
    33
    Tutorial on functions including passing arguments and returning values.

IMN logo majestic logo threadwatch logo seochat tools logo