Riešenie úloh 4. cvičenia


  1. Riešenie:

    body = int(input('Zadaj získaný počet bodov: '))
    hranica = 90
    for znamka in 'ABCDEF':
        if body >= hranica:
            break
        hranica -= 10
    
    if znamka != 'F':
        print('za', body, 'bodov získavaš známku', znamka)
    else:
        print('za', body, 'bodov si nevyhovel a máš známku Fx')
    

  1. Riešenie:

    cislo = int(input('zadaj číslo: '))
    print(cislo, end=' ')
    delitel = 2
    znak = '='
    while cislo > 1:
        if cislo % delitel == 0:
            print(znak, delitel, end=' ')
            cislo //= delitel
            znak = '*'
        else:
            delitel += 1
    print()
    

  1. Riešenie:

    cislo = int(input('zadaj číslo: '))
    cs = 0
    while cislo:
        cifra = cislo % 10
        print(cifra)
        cs += cifra
        cislo = cislo // 10         # cislo //= 10
    print('ciferný súčet =', cs)
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    cislo = int(input('zadaj číslo: '))
    x, y = 350, 150
    while cislo:
        cifra = cislo % 10
        canvas.create_rectangle(x-18, y-18, x+18, y+18, fill='light blue')
        canvas.create_text(x, y, text=cifra, font='arial 30')
        cislo //= 10
        x -= 40
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    cislo = int(input('zadaj číslo: '))
    x, y = 350, 150
    while cislo:
        cifra = cislo % 8
        canvas.create_rectangle(x-18, y-18, x+18, y+18, fill='light blue')
        canvas.create_text(x, y, text=cifra, font='arial 30')
        cislo //= 8
        x -= 40
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = 13
    for i in range(n):
        for j in range(n):
            x = j * 20 + 100
            y = i * 20 + 10
            if i==n//2 or j==n//2:             # po A
            #if i == j or i+j == n-1:          # po B
                farba = 'red'
            else:
                farba = 'white'
            canvas.create_oval(x-8, y-8, x+8, y+8, fill=farba)
    

  1. Riešenie:

    print('zadávaj výšky žiakov')
    dobre = True
    i = 1
    predch = 0
    while True:
        vyska = input(f'   výška {i}. žiaka: ')
        if vyska == '':
            break
        vyska = int(vyska)
        if vyska < predch:
            dobre = False
        predch = vyska
        i += 1
    
    if dobre:
        print('všetci žiaci sú zoradení správne')
    else:
        print('žiaci nie sú správne zoradení')
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 190, 130
    r = 120
    k = 6
    for i in range((r-20) // 3):
        rr = r - i * 3
        if i % k:
            farba = 'black'
        else:
            farba = 'gray'
        canvas.create_oval(x-rr, y-rr, x+rr, y+rr, outline=farba)
    

  1. Riešenie:

    import tkinter
    from random import randint
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    k = 21
    y = 20
    for i in range(10):
        sucet = 0
        x = 20
        while sucet < k:
            h = randint(1, 4)
            canvas.create_oval(x-10, y-10, x+10, y+10, fill='white')
            canvas.create_text(x, y, text=h, font='arial 20')
            sucet += h
            x += 25
        x += 80
        if sucet == k:
            canvas.create_text(x, y, text='HURA', font='arial 20', fill='green')
        else:
            canvas.create_text(x, y, text='ŠKODA', font='arial 20', fill='red')
        y += 25
    

  1. Riešenie:

    import tkinter
    from random import randint
    from math import sqrt
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x0, y0 = 190, 130
    r = 90
    for i in range(2000):
        x = randint(5, 380)
        y = randint(5, 260)
        vzd = sqrt((x-x0)**2 + (y-y0)**2)
        if vzd < r:
            farba = 'red'
        else:
            farba = 'yellow'
        canvas.create_oval(x-5, y-5, x+5, y+5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    from random import randint
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = 7
    for i in range(n):
        x = randint(10, 370)
        y = randint(10, 240)
        canvas.create_oval(x-3, y-3, x+3, y+3, fill='red')
        if i == 0:
            x1 = x2 = x
            y1 = y2 = y
        else:
            if x < x1:
                x1 = x
            if x > x2:
                x2 = x
            if y < y1:
                y1 = y
            if y > y2:
                y2 = y
    canvas.create_rectangle(x1, y1, x2, y2, outline='blue')
    

  1. Riešenie:

    from random import randint
    
    k = 20
    mam = int(input('začínam so sumou: '))
    print('štart ', end='')
    for i in range(1000):
        c1 = randint(1, k)
        c2 = randint(1, k)
        c3 = randint(1, k)
        if c1 == c2 == c3:
            print('+100', end='')
            mam += 99
        elif c1 == c2 or c2 == c3 or c1 == c3:
            print('+5', end='')
            mam += 4
        else:
            print('-1', end='')
            mam -= 1
        if mam <= 0:
            break
    print()
    print('zostalo mi', mam, 'euro')
    

  1. Riešenie:

    cislo = float(input('zadaj číslo: '))
    x = 0
    od, do = 0, cislo
    while abs(2**x - cislo) > 0.0001:
        x = (od + do) / 2
        if 2**x > cislo:
            do = x
        else:
            od = x
    
    print('logaritmus', cislo, 'je', x)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(10000):
        x = random.randint(10, 350)
        y = random.randint(10, 250)
        if y < 90:
            farba = 'black'
        elif y < 170:
            farba = 'red'
        else:
            farba = 'gold'
        canvas.create_oval(x-5, y-5, x+5, y+5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    for i in range(10000):
        x = random.randint(10, 350)
        y = random.randint(10, 250)
        if y < 130:
            if x < y:
                farba = 'blue'
            else:
                farba = 'white'
        else:
            if 130-x > y-130:
                farba = 'blue'
            else:
                farba = 'red'
        canvas.create_oval(x-5, y-5, x+5, y+5, fill=farba, width=0)
    

  1. Riešenie:

    import tkinter
    from random import randint, randrange
    
    sirka = 400
    canvas = tkinter.Canvas(width=sirka)
    canvas.pack()
    
    x, y = 10, 200
    while True:
        a = randint(10, 70)
        b = randint(10, 70)
        if a > b:            # preusporiadanie
            a, b = b, a      # preusporiadanie
        if x + a > sirka:
            break
        farba = f'#{randrange(256**3):06x}'
        canvas.create_rectangle(x, y, x+a, y-b, fill=farba)
        x += a
    

  1. Riešenie:

    import tkinter
    from random import randint
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    r1 = randint(10, 50)
    r2 = randint(10, 50)
    r3 = randint(10, 50)
    
    if r2 < r1:            # preusporiadanie
        r1, r2 = r2, r1    # preusporiadanie
    if r3 < r2:            # preusporiadanie
        r2, r3 = r3, r2    # preusporiadanie
    if r2 < r1:            # preusporiadanie
        r1, r2 = r2, r1    # preusporiadanie
    
    x, y = 190, r1+20
    canvas.create_oval(x-r1, y-r1, x+r1, y+r1, fill='white')
    y += r1 + r2
    canvas.create_oval(x-r2, y-r2, x+r2, y+r2, fill='white')
    y += r2 + r3
    canvas.create_oval(x-r3, y-r3, x+r3, y+r3, fill='white')
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x0, y0 = 190, 130          # stred kružnice
    r = 100                    # polomer kružnice
    farba1, farba2 = 'red', 'yellow'
    n, k = 15, 3               # počet korálikov a konštanta k
    
    uhol = 360 / n
    d = r * sin(radians(uhol / 2))
    for i in range(n):
        if i % k:
            farba = farba1
        else:
            farba = farba2
        x = x0 + r * cos(radians(uhol * i))
        y = y0 + r * sin(radians(uhol * i))
        canvas.create_oval(x-d, y-d, x+d, y+d, fill=farba)