Last active
October 10, 2019 18:08
-
-
Save brabemi/81a59e3a0b92cb4a6f044653ceef6090 to your computer and use it in GitHub Desktop.
PyLadies - Praha (NTK) - podzim 2019 - Lekce 4 – While & Vlastní funkce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def moje_funkce1(): | |
"tato funkce nic nevypíše a nic nevrací" | |
print('-'*20, 'moje_funkce1()', '-'*20) | |
print('Funkce vratila:', moje_funkce1()) | |
def moje_funkce2(): | |
"tato funkce vypíše ahoj a nic nevrací" | |
print('ahoj') | |
print('-'*20, 'moje_funkce2()', '-'*20) | |
print('Funkce vratila:', moje_funkce2()) | |
def moje_funkce3(): | |
"tato funkce nic nevypíše a vrací nic (None), to samé co moje_funkce1" | |
return None | |
print('-'*20, 'moje_funkce3()', '-'*20) | |
print('Funkce vratila:', moje_funkce3()) | |
def moje_funkce4(): | |
"tato funkce nic nevypíše a vrací ahoj" | |
return 'ahoj' | |
print('-'*20, 'moje_funkce4()', '-'*20) | |
print('Funkce vratila:', moje_funkce4()) | |
def moje_funkce5(): | |
"tato funkce vypíše ahoj1 a vrací ahoj2" | |
print('ahoj1') | |
return 'ahoj2' | |
print('-'*20, 'moje_funkce5()', '-'*20) | |
print('Funkce vratila:', moje_funkce5()) | |
navratova_hodnota = moje_funkce5() | |
print(navratova_hodnota*3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def obvod_ctverce(strana): | |
return 4 * strana | |
def obsah_ctverce(strana): | |
return strana ** 2 | |
def vypis_parametry_ctverce(strana): | |
# funkci mohu zavolat v jiné funkci | |
# je celkem běžné složit novou funkci z jiných funkcí | |
obvod = obvod_ctverce(strana) | |
obsah = obsah_ctverce(strana) | |
print('obvod je:', obvod) | |
print('obsah je:', obsah) | |
def obvod_obdelnika(sirka, vyska): | |
"Vrátí obvod obdélníka daných rozměrů" | |
return 2 * (sirka + vyska) | |
vypis_parametry_ctverce(10) | |
vypis_parametry_ctverce(11) | |
vypis_parametry_ctverce(12) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
globalni = 123 | |
def moje_funkce1(): | |
lokalni = 10 | |
# globalni = globalni + 11 #tohle nebude fungovat | |
print('moje_funkce1 lokalni =', lokalni) | |
print('moje_funkce1 globalni =', globalni) | |
def moje_funkce2(): | |
lokalni = 10 | |
globalni = 11 # ve funkci vznikne lokální proměnná se jménem globalni | |
print('moje_funkce2 lokalni =', lokalni) | |
print('moje_funkce2 globalni =', globalni) | |
print('-'*20, 'print()', '-'*20) | |
# print('lokalni =', lokalni) #tohle nebude fungovat | |
print('globalni =', globalni) | |
print('-'*20, 'moje_funkce1()', '-'*20) | |
moje_funkce1() | |
print('-'*20, 'moje_funkce2()', '-'*20) | |
moje_funkce2() | |
print('globalni =', globalni) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nejsmensi_cislo = int(input('Zadej cislo: ')) | |
for pokus in range(4): | |
cislo = int(input('Zadej cislo: ')) | |
if cislo < nejsmensi_cislo: | |
nejsmensi_cislo = cislo | |
print(nejsmensi_cislo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# verze 1 | |
# Napiš funkci, která dostane celé číslo n | |
# pokud je liché vrátí 3*n+1 | |
# pokud je sudé vrátí n//2 | |
# tri_n(1) -> 4 | |
# tri_n(4) -> 2 | |
# tri_n(3) -> 10 | |
def tri_n(n): | |
if n % 2 == 0: | |
# n je sudé | |
return n//2 | |
else: | |
# n je liché | |
return 3*n+1 | |
print('tri_n(1) -> 4', tri_n(1)) | |
print('tri_n(4) -> 2', tri_n(4)) | |
print('tri_n(3) -> 10', tri_n(3)) | |
# verze 2 | |
# napiš funci, která dostane celé číslo n | |
# na číslo n se opakovaně použije funkce tri_n() dokud nevrátí 1 | |
# funkce vrátí, kolik cyklů musela provést než vyšlo 1 | |
# n = 5: tri_n(5) -> 16, tri_n(16) -> 8, tri_n(8) -> 4, tri_n(4) -> 2, tri_n(2) -> 1 | |
# pocet_iteraci(1) -> 0 | |
# pocet_iteraci(2) -> 1 | |
# pocet_iteraci(5) -> 5 | |
# pocet_iteraci(12) -> 9 | |
def pocet_iteraci(n): | |
iteraci = 0 | |
while n != 1: | |
n = tri_n(n) | |
iteraci += 1 | |
return iteraci | |
print('pocet_iteraci(1) -> 0', pocet_iteraci(1)) | |
print('pocet_iteraci(2) -> 1', pocet_iteraci(2)) | |
print('pocet_iteraci(5) -> 5', pocet_iteraci(5)) | |
print('pocet_iteraci(12) -> 9', pocet_iteraci(12)) | |
# verze 3 | |
# napiš funkci, která spočítá počet iterací pro čísla 1-n | |
# funkce bude vypisovat, kolik iterací je potřeba probvést pro které číslo | |
# 1: 0x | |
# 2: 1x | |
# 3: 7x | |
# na závěr vypíše, maximum a u kterého čísla nastalo | |
def nejvice_iteraci(maximum): | |
# pro 1 je 0 iterací | |
max_iteraci = 0 | |
max_cislo = 1 | |
for n in range(1, maximum+1): | |
iteraci = pocet_iteraci(n) | |
if iteraci > max_iteraci: | |
max_iteraci = iteraci | |
max_cislo = n | |
print('Nejvíce bylo', max_iteraci, 'iterací pro číslo', max_cislo) | |
nejvice_iteraci(1) | |
nejvice_iteraci(10) | |
nejvice_iteraci(100) | |
nejvice_iteraci(1000) | |
# verze 4 | |
# pokud se nudíš zadej do google "3n+1 problem" | |
# např: https://cs.wikipedia.org/wiki/Collatz%C5%AFv_probl%C3%A9m | |
# více: https://www.codewars.com/kata/search/my-languages?q=collatz&beta=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randrange | |
# verze 1 | |
# Napiš program, který bude házet tak dlouho, dokud nepadne 6. | |
# Poté co padne vypíše, kolikrát musel hodit | |
pocet_hodu = 0 | |
while True: | |
hod = randrange(1, 7) | |
pocet_hodu = pocet_hodu + 1 | |
if hod == 6: | |
break | |
print('Bylo třeba hodit', pocet_hodu, 'x') | |
# verze 2 | |
# orig: uprav program, aby fungoval pro více kostek (např. 3 kostky, 6 na všech) | |
# edit: uprav program, aby fungoval pro více kostek (např. 3 kostky, 6 na všech kostkách najednou) | |
pocet_hodu = 0 | |
pocet_kostek = 3 | |
while True: | |
pocet_hodu = pocet_hodu + 1 | |
pocet_6 = 0 | |
for kostka in range(pocet_kostek): | |
hod = randrange(1, 7) | |
if hod == 6: | |
pocet_6 += 1 | |
if pocet_6 == pocet_kostek: | |
break | |
print('Bylo třeba hodit', pocet_hodu, 'x') | |
# verze 3 | |
# uprav program, aby uživatel mohl zadat, které číslo na kostce má padat | |
zvolene_cislo = int(input('Zadej číslo, které má padnout (1-6): ')) | |
pocet_hodu = 0 | |
pocet_kostek = 3 | |
while True: | |
pocet_hodu = pocet_hodu + 1 | |
spravne_cislo = 0 | |
for kostka in range(pocet_kostek): | |
hod = randrange(1, 7) | |
if hod == zvolene_cislo: | |
spravne_cislo += 1 | |
if spravne_cislo == pocet_kostek: | |
break | |
print('Bylo třeba hodit', pocet_hodu, 'x') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randrange | |
while True: | |
mince = input('pana nebo orel? ') | |
hod = randrange(2) | |
if mince == 'pana': | |
mince = 1 | |
elif mince == 'orel': | |
mince = 0 | |
elif mince == 'konec': | |
break | |
else: | |
print('Tohle neznám') | |
continue | |
if mince == hod: | |
print('Vyhrál jsi') | |
else: | |
print('Zkus to znovu') | |
print('Konec hry') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment