Riešenie 16. cvičenia


  1. Výstup:

    zviera pes  meno Dunčo a robí haf-haf
    zviera mačka  meno Mica a robí mnau-mnau
    zviera pes  meno Bono a robí vrr-vrr
    zviera kačka  meno Gréta a robí ga-ga
    ...
    

  1. Riešenie:

    class Zviera:
        def __init__(self, meno):
            self._meno = meno.capitalize()
    
        def __str__(self):
            return f'zviera {self.typ} má meno {self.meno} a robí {self.zvuk}'
    
        def daj_meno(self):
            return self._meno
    
        def zmen_meno(self, meno):
            self._meno = meno.capitalize()
    
        def daj_typ(self):
            return self._typ
    
        def daj_zvuk(self):
            return self._zvuk
    
        def zmen_zvuk(self, zvuk):
            if '-' in zvuk:
                self._zvuk = zvuk
            else:
                self._zvuk = zvuk + '-' + zvuk
    
        typ = property(daj_typ)
        meno = property(daj_meno, zmen_meno)
        zvuk = property(daj_zvuk, zmen_zvuk)
    
    class Pes(Zviera):
        _typ = 'pes'
        _zvuk = 'haf-haf'
    
    class Macka(Zviera):
        _typ = 'mačka'
        _zvuk = 'mnau-mnau'
    
    class Kacka(Zviera):
        _typ = 'kačka'
        _zvuk = 'ga-ga'
    

  1. Riešenie:

    class Ucet:
        def __init__(self, meno, suma=0):
            self.meno, self.suma = meno, suma
    
        def __str__(self):
            return f'ucet {self.meno} -> {self.suma} euro'
    
        def stav(self):
            return self.suma
    
        def vklad(self, suma):
            self.suma += suma
    
        def vyber(self, suma):
            if suma <= 0:
                return 0
            suma = min(self.suma, suma)
            self.suma -= suma
            return suma
    

  1. Riešenie:

    class Ucet:
        def __init__(self, meno, suma=0):
            self.meno, self.suma = meno, suma
    
        def __str__(self):
            return f'ucet {self.meno} -> {self.suma} euro'
    
        def stav(self):
            return self.suma
    
        def vklad(self, suma):
            self.suma += suma
    
        def vyber(self, suma):
            if suma <= 0:
                return 0
            suma = min(self.suma, suma)
            self.suma -= suma
            return suma
    
    class UcetHeslo(Ucet):
        def __init__(self, meno, heslo, suma=0):
            #self.meno, self.suma = meno, suma
            super().__init__(meno, suma)
            self.heslo = heslo
    
        def __str__(self):
            return f'ucet {self.meno} -> {self.suma} euro'
    
        def vklad(self, suma):
            if self.heslo == input(f'heslo pre {self.meno}? '):
                super().vklad(suma)
            else:
                print('chybne heslo')
    
        def vyber(self, suma):
            if self.heslo == input(f'heslo pre {self.meno}? '):
                return super().vyber(suma)
            else:
                print('chybne heslo')
    

  1. Riešenie:

    import turtle
    import random
    
    class MojaTurtle(turtle.Turtle):
        def __init__(self, x=0, y=0):
            super().__init__()
            self.speed(0)
            self.pu()
            self.setpos(x, y)
            self.pd()
    
        def domcek(self, dlzka):
            for uhol in 90, 90, 90, 30, 120, -60:
                self.fd(dlzka)
                self.rt(uhol)
    
    zoz = []
    for x in range(-300, 300, 60):
        zoz.append(MojaTurtle(x, 0))
    for t in zoz:
        t.domcek(random.randint(30, 50))
    

  1. Riešenie:

    class MojaTurtle1(MojaTurtle):
        def fd(self, dlzka):
            while dlzka >= 5:
                self.lt(60)
                super().fd(5)
                self.rt(120)
                super().fd(5)
                self.lt(60)
                dlzka -= 5
            super().fd(dlzka)
    
    class MojaTurtle2(MojaTurtle):
        def fd(self, dlzka):
            super().fd(dlzka)
            self.rt(180 - random.randint(-3, 3))
            super().fd(dlzka)
            self.rt(180 - random.randint(-3, 3))
            super().fd(dlzka)
    
    turtle.delay(0)
    zoz = []
    for x in range(-300, 300, 60):
        typ = random.choice((MojaTurtle, MojaTurtle1, MojaTurtle2))
        zoz.append(typ(x, 0))
    for t in zoz:
        t.domcek(random.randint(30, 50))
    

  1. Riešenie:

    import turtle
    
    class Turtle1(turtle.Turtle):
        def trojuholnik(self, strana):
            for i in range(3):
                self.fd(strana)
                self.rt(120)
    
    #---- test -------
    
    turtle.delay(0)
    t = Turtle1()
    for i in range(5):
        t.trojuholnik(150)
        t.lt(72)
    

  1. Riešenie:

    import turtle
    import random
    
    class Turtle1(turtle.Turtle):
        def trojuholnik(self, strana):
            for i in range(3):
                self.fd(strana)
                self.rt(60)
                self.fd(10)
                self.rt(60)
    
    class Turtle2(Turtle1):
        def trojuholnik(self, strana):
            self.fillcolor(f'#{random.randrange(256**3):06x}')
            self.begin_fill()
            super().trojuholnik(strana)
            self.end_fill()
    
    #---- test -------
    
    turtle.delay(0)
    t = Turtle2()
    for i in range(5):
        t.trojuholnik(150)
        t.lt(72)
    

  1. Riešenie:

    import tkinter
    
    class Pero:
        canvas = None
        sirka, vyska = 400, 300
    
        def __init__(self, x=0, y=0):
            if self.canvas is None:
                Pero.canvas = tkinter.Canvas(width=self.sirka, height=self.vyska)
                self.canvas.pack()
            self.x, self.y, self.dolu = x, y, True
    
        def pu(self):
            self.dolu = False
    
        def pd(self):
            self.dolu = True
    
        def setpos(self, x, y):
            if self.dolu:
                self.canvas.create_line(self.x, self.y, x, y)
            self.x, self.y = x, y
    
    #---- test -------
    
    p1 = Pero(100, 200)
    p2 = Pero(200, 150)
    
    p1.setpos(150, 200)
    p1.setpos(150, 150)
    p1.setpos(100, 150)
    p1.setpos(100, 200)
    
    p2.setpos(230, 150)
    p2.setpos(230, 120)
    p2.setpos(200, 120)
    p2.setpos(200, 150)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    class Pero:
        canvas = None
        sirka, vyska = 400, 300
    
        def __init__(self, x=0, y=0):
            if self.canvas is None:
                Pero.canvas = tkinter.Canvas(width=self.sirka, height=self.vyska)
                self.canvas.pack()
            self.x, self.y, self.dolu = x, y, True
    
        def pu(self):
            self.dolu = False
    
        def pd(self):
            self.dolu = True
    
        def setpos(self, x, y):
            if self.dolu:
                self.canvas.create_line(self.x, self.y, x, y)
            self.x, self.y = x, y
    
    
    class Korytnacka(Pero):
        def __init__(self):
            super().__init__(self.sirka//2, self.vyska//2)
            self.uhol = 0
    
        def lt(self, uhol):
            self.uhol = (self.uhol - uhol) % 360
    
        def rt(self, uhol):
            self.uhol = (self.uhol + uhol) % 360
    
        def fd(self, dlzka):
            x = self.x + dlzka * cos(radians(self.uhol))
            y = self.y + dlzka * sin(radians(self.uhol))
            self.setpos(x, y)
    
    #---- test -------
    
    t = Korytnacka()
    for i in range(1, 200, 2):
        t.fd(i)
        t.lt(89)
    

  1. Riešenie:

    class Kor1(Korytnacka):
        def strom(self, n, d):
            self.fd(d)
            if n > 0:
                self.lt(40)
                self.strom(n - 1, d * 0.6)
                self.rt(90)
                self.strom(n - 1, d * 0.7)
                self.lt(50)
            self.fd(-d)
    
    #---- test -------
    
    t = Kor1()
    t.pu()
    t.setpos(200, 280)
    t.pd()
    t.lt(90)
    t.strom(5, 100)
    

  1. Riešenie:

    class MojaKor(Korytnacka):
        def domcek(self, dlzka):
            for uhol in 90, 90, 90, 30, 120, -60:
                self.fd(dlzka)
                self.rt(uhol)
    
        def fd(self, dlzka):
            while dlzka >= 5:
                self.lt(60)
                super().fd(5)
                self.rt(120)
                super().fd(5)
                self.lt(60)
                dlzka -= 5
            super().fd(dlzka)
    
    #---- test -------
    
    MojaKor().domcek(100)