Thursday, November 16, 2017

Mileage and Card Classes

Mileage class

#miles per gallon
# as class

class Mileage:
    def __init__(self, miles, gallons):
        self.miles=miles
        self.gallons=gallons
        

    def setMiles(self, miles):
        self.miles=miles

    def mpg(self):
        self.milesPerGallon=self.miles / self.gallons

    def getMPG(self):
        return self.milesPerGallon

    def __str__(self):
        self.mpg()
        return "Your mpg is " + str(self.getMPG())
    

def main():
    mls = float(input("How many total miles "))
    gals = float(input("How many gallons "))
    mileage=Mileage(mls, gals)
    print (mileage)

main()











    

Class version of Card Class

#Card
class Card:
    def __init__(self, rank, suit):
        self.rank=rank
        self.suit=suit
        self.value=0
        
    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def getValue(self):
        if self.rank > 10:
            self.value=10
        else:
            self.value=self.rank
        return self.value
    
    def __str__(self):
        self.name=""
        su=""
        if self.suit =="d":
            su="diamonds"
        elif self.suit=="h":
            su="hearts"
        elif self.suit=="s":
            su="spades"
        else:
            su ="clubs"
        if self.rank >1 and self.rank< 11:
            self.name=str(self.rank) + " of " + su
        if self.rank==1:
            self.name="the ace of " + su
        if self.rank==11:
            self.name="the jack of " + su
        if self.rank==12:
             self.name="the queen of " + su
        if self.rank==13:
             self.name="the king of " + su
        return self.name
        
        
def main():
    
    c=Card(13,"h")
    print(c)
    print(c.getValue())

main()

    
    





    

Modified version of Card Class

#Card
class Card:
    def __init__(self, rank, suit):
        self.rank=rank
        self.suit=suit
        self.value=0
       
        
    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def getValue(self):
        if self.rank > 10:
            self.value=10
        else:
            self.value=self.rank
        return self.value

    def getSuit(self):
        self.su=""
        if self.suit =="d":
            self.su="diamonds"
        elif self.suit=="h":
            self.su="hearts"
        elif self.suit=="s":
            self.su="spades"
        else:
            self.su ="clubs"
        return self.su

    
    
    def __str__(self):
        
        if self.rank >1 and self.rank< 11:
            self.name=str(self.rank) + " of " + self.getSuit()
        if self.rank==1:
            self.name="the ace of " + self.getSuit()
        if self.rank==11:
            self.name="the jack of " + self.getSuit()
        if self.rank==12:
             self.name="the queen of " + self.getSuit()
        if self.rank==13:
             self.name="the king of " + self.getSuit()
        return self.name
        
        
def main():
    
    c=Card(13,"h")
    print(c)
    print(c.getValue())

main()

    
    





    

No comments:

Post a Comment