Sign in to follow this  
Kevin

Python Beginner Tutorial 9 - Introduction to Classes (Object Oriented Programming)

Recommended Posts

This tutorial covers an introduction to Object Oriented Programming in Python. After watching this video you should feel comfortable creating a basic class. This includes a constructor (__init__ function), and class methods. You should feel comfortable using parameters in a class constructor as well as class methods. Class methods should be able to access class properties using self. You should also understand that the 'self' variable needs to be passed into all methods within a class in python.

Files:

Starting Code: Start_Here.py

rps.py: rps.py

rps_annotated.py: rps_annotated.py

Presentation - Introduction to Object Oriented Programming.pdf: Python_ObjectOrientedIntro.pdf

Annotated Code:

Quote

class Game:
    def __init__(self, newPlayerName):
		#Define and set properties for the class
		self.playerName = newPlayerName
        self.playerHand = "NA"
        self.botHand = "NA"
        self.winner = "NA"
		#Print out a message so we know when the constructor has run
        print("New instance of game class for " + self.playerName)

	#Note: This function was copied and only modified to work in our class. 
	#We are going to work on cleaning this up and improving our Game class in future videos.
    def runGame(self):
        if ((self.playerHand == 'rock' or self.playerHand == 'Rock') and self.botHand == 1):
            self.winner = 'You win'
        elif ((self.playerHand == 'rock' or self.playerHand == 'Rock') and self.botHand == 2):
            self.winner = 'You tie.'
        elif ((self.playerHand == 'rock' or self.playerHand == 'Rock') and self.botHand == 3):
            self.winner = 'You lose.'
        elif ((self.playerHand == 'paper' or self.playerHand == 'Paper') and self.botHand == 1):
            self.winner = 'You lose.'
        elif ((self.playerHand == 'paper' or self.playerHand == 'Paper') and self.botHand == 2):
            self.winner = 'You win!'
        elif ((self.playerHand == 'paper' or self.playerHand == 'Paper') and self.botHand == 3):
            self.winner = 'You tie.'
        elif ((self.playerHand == 'scissors' or self.playerHand == 'Scissors') and self.botHand == 1):
            self.winner = 'You tie.'
        elif ((self.playerHand == 'scissors' or self.playerHand == 'Scissors') and self.botHand == 2):
            self.winner = 'You lose.'
        elif ((self.playerHand == 'scissors' or self.playerHand == 'Scissors') and self.botHand == 3):
            self.winner = 'You win!'
        else:
            self.winner = 'INVALID input, try again'


def main():
	#Create a new instance of our game by calling the constructor for the game class.
    myGame = Game('Kevin')
	#Print all the variables in the object by using the instance 'myGame' and the '.' dot to acess methods
	#and variables beloning to that object (instance).
	print(myGame.playerName)
    print(myGame.winner)
    print(myGame.botHand)
    print(myGame.playerHand)

	#Set the hand of the bod and player using the '.' and the assignment operator '='.
	#Works just like any other variable except these variables are contained within our object.
	#Think of these variables living inside the 'myGame' box.
    myGame.botHand = 1
    myGame.playerHand = 'paper'
	#Run our runGame method. The winner will be set in myGame.winner so we can then print it
    myGame.runGame()
    print(myGame.winner)
	
	#Create another instance of Game (to show that we can)
	otherGame = Game('Zibzo')
	#Run the other game
	otherGame.runGame()
	#This should print invalid input as we never set the player/bot hands
	print(otherGame.winner)

 

Starting Code:

Quote

def main():

    import random
    botHand = random.randint(1,3)

        
    userHand = input("Pick rock, paper or scissors ")
    print("You pick " + userHand)
    if(botHand == 1):
        print("Computer picks scissors")
    elif(botHand == 2):
        print("Computer picks rock")
    elif(botHand == 3):
        print("Computer picks paper")
    import time
    time.sleep(2)
    if ((userHand == 'rock' or userHand == 'Rock')and botHand == 1):
        print('You win')
    elif ((userHand == 'rock' or userHand == 'Rock')and botHand == 2):
        print('You tie.')
    elif ((userHand == 'rock' or userHand == 'Rock')and botHand == 3):
        print('You lose.')
    elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 1):
        print('You lose.')
    elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 2):
        print('You win!')
    elif ((userHand == 'paper' or userHand == 'Paper')and botHand == 3):
        print('You tie.')
    elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 1):
        print('You tie.')
    elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 2):
        print('You lose.')
    elif ((userHand == 'scissors' or userHand == 'Scissors')and botHand == 3):
        print('You win!')
    else:
        print('INVALID input, try again')
        import time
        time.sleep(1)
        main()
while (1==1):
    main()
    import time
    time.sleep(1)
    input('Press enter to start again')

 

 

Share this post


Link to post
Share on other sites
def contryStartA(allA):

    a1 = "Afghanistan\n"
    a2 = "Albania\n"
    a3 = "Algeria\n"
    a4 = "Andorra\n"
    a5 = "Angola\n"
    a6 = "Antigua_Barbuda\n"
    a7 = "Argentina\n"
    a8 = "Armenia\n"
    a9 = "Australia\n"
    a10 = "Austria\n"
    a11="Azerbaijan\n"
    allA = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 +a10 + a11
    print(allA)


def contryStartB(allB):
    b1 = "Belarus\n"
    b2 = "Belgium\n"
    b3 = "Belize\n"
    b4 = "Bolivia\n"
    b5 = "Brazil\n"
    b6 = "Bulgaria\n"
    allB = b1 + b2 + b3 + b4 + b5 + b6
    print(allB)


def contryStartC(allC):
    c1 = "Cambodia\n"
    c2 = "Cameroon\n"
    c3 = "Canada\n"
    c4 = "Central\n"
    c5 = "Chile\n"
    c6 = "China\n"
    c7 = "Colombia\n"
    c8 = "Comoros\n"
    c9 = "Costa\n "
    c10 = "Croatia\n"
    c11 = "Cuba\n"
    allC = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11
    print(allC)


def contryStartD(allD):
    d1 ='Denmark\n'
    d2 ='Dominica\n'
    d3 ='Dominican\n'
    allD = d1 + d2 + d3
    print(allD)


def contryStartE(allE):
   e1 = 'Ecuador\n'
   e2 = 'Egypt\n'
   e3 = 'El\n'
   e4 = 'Eritrea\n'
   e5 = 'Ethiopia\n'
   allE = e1 + e2 + e3 + e4 + e5 
   print(allE)


def contryStartF(allF):
    f1 = 'Fiji\n'
    f2 = 'Finland\n'
    f3 = 'France\n'
    allF = f1 + f2 + f3
    print(allF)


def contryStartG(allG):
    g1 = 'Gabon\n'
    g2 = 'Gambia\n'
    g3 = 'Georgia\n'
    g4 = 'Germany\n'
    g5 = 'Ghana\n'
    g6 = 'Greece\n'
    g7 = 'Grenada\n'
    g8 = 'Guatemala\n'
    g9 = 'Guinea\n'
    g10 = 'Guinea - Bissau\n'
    g11 = 'Guyana\n'
    allG = g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9 + g10 + g11
    print(allG)


def contryStartH(allH):
    h1 = 'Haiti\n'
    h2 = 'Honduras\n'
    h3 = 'Hungary\n'
    allH = h1 + h2 + h3
    print(allH)


def contryStartI(allI):

    i1 = 'Iceland'
    i2 = 'India\n'
    i3 = 'Indonesia\n'
    i4 = 'Iran\n'
    i5 = 'Iraq\n'
    i6 = 'Ireland\n'
    i7 = 'Israel\n'
    i8 = 'Italy\n'
    allI = i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8
    print(allI)


def contryStartj(allJ):
    j1 = 'Jamaica'
    j2 = 'Japan\n'
    j3 = 'Jordan\n'
    allJ = j1+j2+j3
    print(allJ)


def contryStartK(allK):
    k1 = 'Kazakhstan'
    k2 = 'Kenya\n'
    k3 = 'Kiribati\n'
    k4 = 'Kosovo\n'
    k5 = 'Kuwait\n'
    k6 = 'Kyrgyzstan\n'
    allK = k1 + k2 + k3 + k4 + k5 + k6
    print(allK)


def contryStartl(allL):
    l1 = 'Laos'
    l2 = 'Latvia\n '
    l3 = 'Lebanon\n'
    l4 = 'Lesotho\n'
    l5 = 'Liberia\n'
    l6 = 'Libya\n'
    l7 = 'Liechtenstein\n'
    l8 = 'Lithuania\n'
    l9 = 'Luxembourg\n'
    allL = l1+l2+l3+l4+l5+l6+l7+l8+l9
    print(allL)


def contryStartM(allM):
    m1 = 'Macedonia'
    m2 = '\nMadagascar'
    m3 = '\nMalawi'
    m4 = '\nMalaysia'
    m5 = '\nMaldives'
    m6 = '\nMali'
    m7 = '\nMalta'
    m8 = '\nMarshall'
    m9 = '\nIslands'
    m10 = '\nMauritania'
    m11 = '\nMauritius'
    m12 = '\nMexico'
    m13 = '\nMicronesia'
    m14 = '\nMoldova'
    m15 = '\nMonaco'
    m16 = '\nMongolia'
    m17 = '\nMontenegro'
    m18 = '\nMorocco'
    m19 = '\nMozambique'
    allM = m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11+m12+m13+m14+m15+m16+m17+m18+m19
    print(allM)


def contryStartN(allN):
    n1 = 'Namibia'
    n2 = 'Nauru\n'
    n3 = 'Nepal\n'
    n4 = 'Netherlands\n'
    n5 = 'New\n'
    n6 = 'Zealand\n'
    n7 = 'Nicaragua\n'
    n8 = 'Niger\n'
    n9 = 'Nigeria\n'
    n10 = 'North\n'
    n11 = 'Korea\n'
    n12 = 'Norway\n'
    allN = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10+n11+n12
    print(allN)


def contryStartO(allO):
    p1 = "Oman"
    allO = p1
    print(allO)


def contryStartP(allP):
    p1 = 'Pakistan'
    p2 = 'Palau\n'
    p3 = 'Palestine\n'
    p4 = 'Panama\n'
    p5 = 'Papua\n'
    p6 = 'New\n'
    p7 = 'Guinea\n'
    p8 = 'Paraguay\n'
    p9 = 'Peru\n'
    p10 = 'Philippines\n'
    p11 = 'Poland\n'
    p12 = 'Portugal\n'
    allP = p1+p2+p3+p4+p5+p6+p7+p8+p9+p10+p11+p12
    print(allP)


def contryStartQ(allQ):
    q1 = 'Qatar'
    allQ = q1
    print(allQ)


def contryStartR(allR):
    r1 = 'Romania'
    r2 = 'Russia\n'
    r3 = 'Rwanda\n'
    allR = r1 + r2 + r3
    print(allR)


def contryStartS(allS):

    s1 ="San"
    s2 = "Marino\n"
    s3 = "Sao\n"
    s4 = "Tome'and'Principe\n"
    s5 = "Saudi\n"
    s6 = "Arabia\n"
    s7 = "Senegal\n"
    s8 = "Serbia\n"
    s9 = "Seychelles\n"
    s10 = "Sierra\n"
    s11 = "Leone\n"
    s12 = "Singapore\n"
    s13 = 'Slovakia\n'
    s14 = 'Slovenia\n'
    s15 = 'Solomon\n'
    s16 = 'Islands\n'
    s17 = 'Somalia\n'
    s18 = 'SouthAfrica\n'
    s19 = 'South Korea\n'
    s20 = 'South Sudan\n'
    s21 = 'Spain\n'
    s22 = 'Sri\n'
    s23 = 'Lanka\n'
    s24 = 'Sudan\n'
    s25 = 'Suriname\n'
    s26 = 'Swaziland\n'
    s27 = 'Sweden\n'
    s28 = 'Switzerland\n'
    s29 = 'Syria\n'
    allS = s1+s2+s3+s4+s5+s6+s7+s8+s9+s10+s11+s12+s13+s14+s15+s16+s17+s18+s19+s20+s21+s22+s23+s24+s25+s26+s27+s28+s29
    print(allS)


def contryStartT(allT):
    t1 = 'Taiwan'
    t2 = 'Tajikistan\n'
    t3 = 'Tanzania\n'
    t4 = 'Thailand\n'
    t5 = 'Timor - Leste\n'
    t6 = 'Togo\n'
    t7 = 'Tonga\n'
    t8 = "Trinidad'and'Tobago\n"
    t9 = 'Tunisia\n'
    t10 = 'Turkey\n'
    t11 = 'Turkmenistan\n'
    t12 = 'Tuvalu\n'
    allT = t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12
    print(allT)


def contryStartU(allU):
    u1 = 'Uganda'
    u2 = 'Ukraine'
    u3 = 'United'
    u4 = 'Arab Emirates\n'
    u5 = 'UnitedKingdom(UK)\n'
    u6 = 'United States of America(USA)\n'
    u7 = 'Uruguay\n'
    u8 = 'Uzbekistan\n'
    allU = u1+u2+u3+u4+u5+u6+u7+u8
    print(allU)


def contryStartV(allV):
    v1 = 'Vanuatu'
    v2 = '\nVatican City Holy See'
    v3 = '\nVenezuela'
    v4 = '\nVietnam'
    allV = v1 + v2 + v3 + v4
    print(allV)


def contryStartY(allY):
    y1 = 'Yemen'
    allY = y1
    print(allY)


def contryStartZ(allZ):
    z1 = 'Zambia'
    z2 = '\nZimbabwe\n'
    allZ = z1+z2
    print(allZ)

    
def main1():
    contrYa = input(" press the namber 1 if your contry start with a(a1.,b2.,c3.,d4.,e5.,f6.,g7.,h8.,i9.,j10.,k11.,l12.,"
                    "m13.,n14.,o15.,p16.,q17.,r18.,s19.,t20.,u21.,v22.,y23.,z24.,")
    if (contrYa == '1'):
        print(contryStartA(allA=str))
    elif(contrYa == '2'):
        print(contryStartB(allB=str))
    elif (contrYa == '3'):
        print(contryStartC(allC=str))
    elif (contrYa == '4'):
        print(contryStartD(allD=str))
    elif(contrYa == '5'):
        print(contryStartE(allE=str))
    elif(contrYa == '6'):
        print(contryStartF(allF=str))
    elif (contrYa == '7'):
        print(contryStartG(allG=str))
    elif (contrYa == '8'):
        print(contryStartH(allH=str))
    elif (contrYa == '9'):
        print(contryStartI(allI=str))
    elif (contrYa == '10'):
        print(contryStartj(allJ=str))
    elif (contrYa == '11'):
        print(contryStartK(allK=str))
    elif (contrYa == '12'):
        print(contryStartl(allL=str))
    elif (contrYa == '13'):
        print(contryStartM(allM=str))
    elif (contrYa == '14'):
        print(contryStartN(allN=str))
    elif (contrYa == '15'):
        print(contryStartO(allO=str))
    elif (contrYa == '16'):
        print(contryStartP(allP=str))
    elif (contrYa == '17'):
        print(contryStartQ(allQ=str))
    elif (contrYa == '18'):
        print(contryStartR(allR=str))
    elif (contrYa == '19'):
        print(contryStartS(allS=str))
    elif (contrYa == '20'):
        print(contryStartT(allT=str))
    elif (contrYa == '21'):
        print(contryStartU(allU=str))
    elif (contrYa == '22'):
        print(contryStartV(allV=str))
    elif (contrYa == '23'):
        print(contryStartY(allY=str))
    elif (contrYa == '24'):
        print(contryStartZ(allZ=str))





main1()

 

Share this post


Link to post
Share on other sites
On 5/5/2018 at 4:22 AM, yogev_shushan said:

I'm already waiting for the next lesson
 good job dude :) 

 

Hello there,
I think your Python Training has been completed and you have a great knowledge of Python Language. 
So, can you tell me which programming Language is an implementation of Python programming language designed to run on Java Platform?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this