Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| efinf:blcks2017:jython:lektionen [2017/09/20 20:09] – [L11] Simon Knaus | efinf:blcks2017:jython:lektionen [2017/10/02 20:05] (current) – Simon Knaus | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== L3 ==== | ||
| + | < | ||
| + | from gturtle import * | ||
| + | makeTurtle() | ||
| + | # Turtle verstecken | ||
| + | ht() | ||
| + | s = 2 | ||
| + | repeat 100: | ||
| + | forward(s) | ||
| + | left(90) | ||
| + | # turtle weiter als 20 vom ursprung und weniger weit als 30: bitte grün | ||
| + | if distance(0, | ||
| + | setPenColor(" | ||
| + | # sonst einfach blau | ||
| + | else: | ||
| + | setPenColor(" | ||
| + | s = s + 2 | ||
| + | </ | ||
| + | |||
| + | |||
| + | <code python variable.py> | ||
| + | from gturtle import * | ||
| + | |||
| + | makeTurtle() | ||
| + | |||
| + | # Variable s für die Seitenlänge | ||
| + | s = 2 | ||
| + | # Funktion segment mit zwei Parametern width und angle | ||
| + | def segment(width, | ||
| + | forward(width) | ||
| + | left(angle) | ||
| + | | ||
| + | repeat 20: | ||
| + | segment(s) | ||
| + | s = s + 2 | ||
| + | </ | ||
| + | |||
| + | ==== L4 ==== | ||
| + | <code python fibonacci.py> | ||
| + | # Rekursive Definition Fibonacci Zahlen | ||
| + | def fibonacci(n): | ||
| + | #f_0 = 1 | ||
| + | if n == 0: | ||
| + | return(0) | ||
| + | #f_1 = 1 | ||
| + | elif n == 1: | ||
| + | return(1) | ||
| + | # f_n = f_n-1 + f_n-2 | ||
| + | else: | ||
| + | return(fibonacci(n-1)+fibonacci(n-2)) | ||
| + | | ||
| + | |||
| + | |||
| + | #1.618 goldener schnitt | ||
| + | goldensection = (sqrt(5)+1)/ | ||
| + | n = 2 | ||
| + | |||
| + | #solange fibonacci ausrechnen, bis Quotient auf 0.0001 am goldenen Schnitt ist. | ||
| + | while abs(fibonacci(n)/ | ||
| + | print(fibonacci(n)) | ||
| + | n += 1 #n wird um 1 erhöht äquivalent zu n = n+1 | ||
| + | |||
| + | # Frage: ist das '' | ||
| + | </ | ||
| + | |||
| + | ==== L5 ==== | ||
| + | <code python keycode.py> | ||
| + | from gturtle import * | ||
| + | # notwending, weil getKeyCodeWait() aus gturtle ist. | ||
| + | makeTurtle() | ||
| + | |||
| + | print(getKeyCodeWait()) | ||
| + | |||
| + | # oder | ||
| + | |||
| + | while True: | ||
| + | print(getKeyCodeWait()) | ||
| + | </ | ||
| + | <code python globale_variablen.py> | ||
| + | a = 12 #globale Variable | ||
| + | |||
| + | def funprint(): | ||
| + | print(a) # ' | ||
| + | b=2*a # wird lokal verändert | ||
| + | print(b) | ||
| + | |||
| + | def funchange(): | ||
| + | a=14 #lokale Variable, ändert globale Variable a nicht | ||
| + | print(a) | ||
| + | |||
| + | def funchangeglobal(): | ||
| + | global a #welche globalen Variablen möchte ich in Funktion verwenden | ||
| + | a = 14 #verändert globale Variable a | ||
| + | print(a) | ||
| + | |||
| + | funprint() | ||
| + | print(a) | ||
| + | funchange() | ||
| + | print(a) | ||
| + | funchangeglobal() | ||
| + | print(a) | ||
| + | #Achtung: Globale Variablen zurückhaltend einsetzen. | ||
| + | </ | ||
| + | ==== L6 ==== | ||
| + | Take-Aways Hausaufagben: | ||
| + | * Namen von Funktionen werden klein geschrieben. | ||
| + | * Konvention von Python: Falls notwendig mit Underscore zur Klärung. Bsp '' | ||
| + | * Konvention von TigerJython: | ||
| + | * Generelle Praxis: " | ||
| + | * If-Statements: | ||
| + | * Nicht per Mail abgeben. | ||
| + | * Divisionsrest: | ||
| + | |||
| + | |||
| + | Besprechung Aufgabe 2.10: | ||
| + | <code python turtle_exit.py> | ||
| + | from gturtle import * | ||
| + | |||
| + | # Keycodes der Tasten definieren | ||
| + | XLETTER = 88 | ||
| + | LEFT = 37 | ||
| + | RIGHT = 39 | ||
| + | UP = 38 | ||
| + | DOWN = 40 | ||
| + | |||
| + | # Funktionsdefinition Key Listener | ||
| + | def onKeyPressed(key): | ||
| + | | ||
| + | if key == LEFT: | ||
| + | setHeading(-90) | ||
| + | elif key == RIGHT: | ||
| + | | ||
| + | elif key == UP: | ||
| + | setHeading(0) | ||
| + | elif key == DOWN: | ||
| + | | ||
| + | elif key == XLETTER: | ||
| + | | ||
| + | |||
| + | |||
| + | # Turtle initalisieren | ||
| + | makeTurtle(keyPressed = onKeyPressed) | ||
| + | |||
| + | # globale Variable zum Schlaufen--Abbruch | ||
| + | stopit = False | ||
| + | |||
| + | # halbe Breite und halbe Höhe | ||
| + | h = getPlaygroundHeight()/ | ||
| + | w = getPlaygroundWidth()/ | ||
| + | |||
| + | # Turtle bewegen | ||
| + | while True: | ||
| + | forward(10) | ||
| + | print(getPos()) #Position der Turtle | ||
| + | # wenn eines der drei Abbruch-Kriterien erfüllt ist: break | ||
| + | if stopit or abs(getX()> | ||
| + | break | ||
| + | |||
| + | # wenn Programm regulär beendet wird, wird "habe fertig" | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ==== L7 ==== | ||
| + | ==== L8 ==== | ||
| + | === Hausaufgaben === | ||
| + | Take-Aways Hausaufagben: | ||
| + | * Unnötige Operationen / Iteration etc. vermeiden | ||
| + | * Standard-Variablen für Leserlichkeit verwenden (i,j,k anstelle von I, II, III) | ||
| + | * Aufgaben genau lesen resp. rückfragen. | ||
| + | == Strukturiertes Progammieren == | ||
| + | <code python stern.py> | ||
| + | import math | ||
| + | from gpanel import * | ||
| + | makeGPanel(-10, | ||
| + | |||
| + | def stern(x, y, r): | ||
| + | | ||
| + | x + math.sqrt(3)/ | ||
| + | x, y + r); | ||
| + | | ||
| + | x + math.sqrt(3)/ | ||
| + | x, y - r); | ||
| + | |||
| + | |||
| + | stern(0, 0, 3) | ||
| + | </ | ||
| + | == Farbwechsel == | ||
| + | <code python changecolor.py> | ||
| + | from gturtle import * | ||
| + | |||
| + | # Keycodes der Tasten definieren | ||
| + | XLETTER = 88 | ||
| + | LEFT = 37 | ||
| + | RIGHT = 39 | ||
| + | UP = 38 | ||
| + | DOWN = 40 | ||
| + | |||
| + | # Funktion, welche die Farbe auf Grund x und y definiert | ||
| + | def changeColor(x, | ||
| + | distance = sqrt(x*x+y*y) | ||
| + | returncolor = " | ||
| + | if(distance < 100): | ||
| + | returncolor = " | ||
| + | elif(distance < 200): | ||
| + | returncolor = " | ||
| + | elif(distance < 300): | ||
| + | returncolor = " | ||
| + | | ||
| + | ## was würde passieren, wenn die Reihenfolge von 300, 200, 100 umgekehrt würde? | ||
| + | return returncolor | ||
| + | | ||
| + | # Funktionsdefinition Key Listener | ||
| + | def onKeyPressed(key): | ||
| + | | ||
| + | if key == LEFT: | ||
| + | setHeading(-90) | ||
| + | elif key == RIGHT: | ||
| + | | ||
| + | elif key == UP: | ||
| + | setHeading(0) | ||
| + | elif key == DOWN: | ||
| + | | ||
| + | elif key == XLETTER: | ||
| + | | ||
| + | |||
| + | |||
| + | # Turtle initalisieren | ||
| + | makeTurtle(keyPressed = onKeyPressed) | ||
| + | |||
| + | # globale Variable zum Schlaufen--Abbruch | ||
| + | stopit = False | ||
| + | |||
| + | # halbe Breite und halbe Höhe | ||
| + | h = getPlaygroundHeight()/ | ||
| + | w = getPlaygroundWidth()/ | ||
| + | |||
| + | # Turtle bewegen | ||
| + | while True: | ||
| + | forward(10) | ||
| + | print(getPos()) #Position der Turtle | ||
| + | #Aufruf der Funktion changeColor | ||
| + | setColor(changeColor(getX(), | ||
| + | # wenn eines der drei Abbruch-Kriterien erfüllt ist: break | ||
| + | if stopit or abs(getX()> | ||
| + | break | ||
| + | |||
| + | # wenn Programm regulär beendet wird, wird "habe fertig" | ||
| + | print(" | ||
| + | </ | ||
| + | == Primzahlen == | ||
| + | <code python primes.py> | ||
| + | # Um Ausführungszeit zu messen | ||
| + | import time | ||
| + | |||
| + | # Halbwegs effiziente Version | ||
| + | def checkPrime1(n, | ||
| + | for i in range(2, | ||
| + | isPrime = True | ||
| + | for j in range(2, | ||
| + | if i%j == 0: | ||
| + | isPrime = False | ||
| + | break | ||
| + | if(isPrime): | ||
| + | if(printout): | ||
| + | print(i) | ||
| + | |||
| + | # Ineffiziente Version | ||
| + | def checkPrime2(n, | ||
| + | for i in range(2, | ||
| + | isPrime = True | ||
| + | for j in range(2,i): | ||
| + | if i%j == 0: | ||
| + | isPrime = False | ||
| + | if(isPrime): | ||
| + | if(printout): | ||
| + | print(i) | ||
| + | |||
| + | |||
| + | |||
| + | ## Zeit messen für checkPrimes1 | ||
| + | t0 = time.clock() | ||
| + | repeat 100: | ||
| + | checkPrime1(1001, | ||
| + | print((time.clock()-t0)) | ||
| + | |||
| + | ## Nochmals messen für checkPrimes2 | ||
| + | t0 = time.clock() | ||
| + | repeat 100: | ||
| + | checkPrime2(1001, | ||
| + | print((time.clock()-t0)) | ||
| + | </ | ||
| + | == Ganzzahl Operationen == | ||
| + | Die Operationen / / und % sind nicht nur praktischer sonder auch einiges schneller: | ||
| + | |||
| + | |||
| + | <code python ganzzahl.py> | ||
| + | # Um Ausführungszeit zu messen | ||
| + | import time | ||
| + | t0 = time.clock() | ||
| + | a = 0 | ||
| + | repeat 1000000: | ||
| + | a = (14/2 == (int(14/ | ||
| + | print((time.clock()-t0)) | ||
| + | |||
| + | t0 = time.clock() | ||
| + | a = 0 | ||
| + | repeat 1000000: | ||
| + | a = (14%2 == 0) | ||
| + | print((time.clock()-t0)) | ||
| + | </ | ||
| + | |||
| + | ==== L10 ==== | ||
| + | === Feebdack === | ||
| + | Coding Style | ||
| + | * Funktionen werden klein geschrieben. Idealerweise auch als Verb und nicht als Subjekt, bsp. '' | ||
| + | * Listennamen sind idealerweise im Plural. Dann ist bereits aus dem Variabelnamen klar, dass es sich um eine Liste handelt. | ||
| + | * Achtung mit mutable und imuutable data types. | ||
| + | |||
| + | |||
| + | === Mutable und immutable data types === | ||
| + | <code python example.py> | ||
| + | x = ' | ||
| + | y = x | ||
| + | print x | ||
| + | # foo | ||
| + | y += ' | ||
| + | print x | ||
| + | # foo | ||
| + | print y | ||
| + | # foobar | ||
| + | |||
| + | x = [1, 2, 3] | ||
| + | y = x | ||
| + | print x | ||
| + | # [1, 2, 3] | ||
| + | y += [3, 2, 1] | ||
| + | print x | ||
| + | # [1, 2, 3, 3, 2, 1] | ||
| + | print y | ||
| + | # [1, 2, 3, 3, 2, 1] | ||
| + | |||
| + | def fun(val): | ||
| + | val = ' | ||
| + | x = ' | ||
| + | print x | ||
| + | # foo | ||
| + | fun(x) | ||
| + | print x | ||
| + | # foo | ||
| + | |||
| + | def fun(val): | ||
| + | val[1]=-1 | ||
| + | |||
| + | x = [1, 2, 3] | ||
| + | print x | ||
| + | # [1, 2, 3] | ||
| + | |||
| + | fun(x) | ||
| + | print x | ||
| + | # [1, -1, 3] | ||
| + | |||
| + | def fun(val): | ||
| + | val = [1,2,3] | ||
| + | val[1] = -2 | ||
| + | |||
| + | x = [1, 2, 3] | ||
| + | print x | ||
| + | # [1, 2, 3] | ||
| + | fun(x) | ||
| + | print x | ||
| + | # [1,2, 3] | ||
| + | </ | ||
| + | Bitte zusätzlich noch diese [[http:// | ||
| + | * Vom Modul '' | ||
| + | * Jeweils eine neue Liste verwenden, d.h. '' | ||
| + | * Das Verhalten <<in Kauf nehmen>> | ||
| + | |||
| + | === Module === | ||
| + | |||
| + | Wir haben bis jetzt schon mit Modulen gearbeitet. Diese haben wir entweder mit '' | ||
| + | <code python efinfo.py> | ||
| + | import sys | ||
| + | def printDoubleLists(lists): | ||
| + | for i in range(len(lists)): | ||
| + | for j in range(len(lists[i])): | ||
| + | sys.stdout.write(str(lists[i][j])) | ||
| + | sys.stdout.write(" | ||
| + | sys.stdout.write(" | ||
| + | </ | ||
| + | Nachher könnte die Funktion wie folgt aufgerufen werden. Achtung '' | ||
| + | <code python> | ||
| + | import efinfo | ||
| + | tiles = [[4, -1, 4, 8], | ||
| + | [16, 32, 64, 128], | ||
| + | [256, -1, -1, 2048], | ||
| + | [-1, -1, -1, -1]] | ||
| + | |||
| + | efinfo.printDoubleLists(tiles) | ||
| + | |||
| + | # oder | ||
| + | from efinfo import * | ||
| + | tiles = [[4, -1, 4, 8], | ||
| + | [16, 32, 64, 128], | ||
| + | [256, -1, -1, 2048], | ||
| + | [-1, -1, -1, -1]] | ||
| + | |||
| + | printDoubleLists(tiles) | ||
| + | </ | ||
| + | === Lernziele === | ||
| + | * Alle Lernziele vom Lehrmittel der bearbeiten Kapitel (d.h., z.B. ohne Rekursion, ohne Objekte im Kapitel 2) | ||
| + | * Zusätzlich dazu: | ||
| + | * Programme mit gechachtelten Schlaufen lesen und schreiben können | ||
| + | * Programme mit geschachtelten Listen lesen und schreiben können | ||
| + | == Beispiel 1 == | ||
| + | Was ist die Ausgabe des untenstehenden Programms? Was die Idee? | ||
| + | <code python> | ||
| + | numbers = [[4, -1, 4, 8], | ||
| + | [16, 32, 64, 128], | ||
| + | [256, -1, -1, 2048], | ||
| + | [-1, -1, 1, -1]] | ||
| + | |||
| + | result = [0]*len(numbers) | ||
| + | for i in range(len(numbers)): | ||
| + | for j in range(len(numbers[i])): | ||
| + | result[i]+=numbers[i][j] | ||
| + | print(result) | ||
| + | </ | ||
| + | == Beispiel 2 == | ||
| + | Was ist die Ausgabe des untenstehenden Programms? | ||
| + | <code python> | ||
| + | def dummyfunction(c, | ||
| + | e = 2+d | ||
| + | e += c | ||
| + | foofunction(c, | ||
| + | print(e) | ||
| + | if e>0: | ||
| + | print(" | ||
| + | else: | ||
| + | print(" | ||
| + | |||
| + | def foofunction(c, | ||
| + | e=0 | ||
| + | for i in range(c,d): | ||
| + | e+=i | ||
| + | print(e) | ||
| + | |||
| + | dummyfunction(2, | ||
| + | </ | ||
| + | === Lösungen === | ||
| + | <code python skeleton.py> | ||
| + | from efinfo import * | ||
| + | import copy | ||
| + | tiles = [[4, -1, 4, 8], | ||
| + | [16, 32, 64, 128], | ||
| + | [256, -1, -1, 2048], | ||
| + | [-1, -1, -1, -1]] | ||
| + | |||
| + | def shiftAndCollapseLine(inline): | ||
| + | ##Idee: Kippen, ersetzen, kippen | ||
| + | | ||
| + | #einmal kippen | ||
| + | #idee: alle nicht -1 sammeln, dann -1 wieder anhängen | ||
| + | | ||
| + | #leere tiles | ||
| + | temp = [] | ||
| + | #sammeln | ||
| + | for j in inline: | ||
| + | if j != -1: | ||
| + | temp.append(j) | ||
| + | | ||
| + | #anhängen: Python' | ||
| + | temp += [-1] * inline.count(-1) | ||
| + | #tiles umdrehen | ||
| + | temp.reverse() | ||
| + | | ||
| + | #identicshe zellen verdoppeln und durch -1 ersetzen | ||
| + | for j in range(len(temp) - 1): | ||
| + | if temp[j] == temp[j + 1] and temp[j] != -1: | ||
| + | temp[j+1] = 2 * temp[j] | ||
| + | temp[j] = -1 | ||
| + | inline=[] | ||
| + | | ||
| + | #wieder sammeln und anhängen | ||
| + | for j in temp: | ||
| + | if j != -1: | ||
| + | inline.append(j) | ||
| + | inline+=[-1] * temp.count(-1) | ||
| + | #tiles umdrehen | ||
| + | inline.reverse() | ||
| + | return(inline) | ||
| + | |||
| + | def shiftAndCollapseGrid(tiles): | ||
| + | # ' | ||
| + | temp = [-1]*len(tiles) | ||
| + | for i in range(len(tiles)): | ||
| + | temp[i] = shiftAndCollapseLine(tiles[i]) | ||
| + | return(temp) | ||
| + | | ||
| + | def rotateGrid(tiles): | ||
| + | |||
| + | returnlist = copy.deepcopy(tiles) | ||
| + | #sehr Pythonish | ||
| + | return(list(map(list, | ||
| + | |||
| + | printDoubleLists(tiles) | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | repeat 4: | ||
| + | tiles = rotateGrid(tiles) | ||
| + | printDoubleLists(tiles) | ||
| + | </ | ||
| + | |||
| + | ==== L11 ==== | ||
| + | Auf Grund der Diskussionen in den Gruppen nochmals ein Brush-up: | ||
| + | |||
| + | === Scoping (Lebensdauer von Variablen) === | ||
| + | <code python> | ||
| + | # Globale Variable a | ||
| + | a = 0 | ||
| + | |||
| + | if a == 0: | ||
| + | # Immer noch eine globale Variable b | ||
| + | b = 1 | ||
| + | |||
| + | def myFunction(c): | ||
| + | # d ist eine lokale Variable, c ist Argument der Funktion | ||
| + | d = 3 | ||
| + | print(c) | ||
| + | print(d) | ||
| + | return(d+c) | ||
| + | |||
| + | # Funktionsaufruf mit Arugment 7, der Rückgabewert wird der _globalen_ Variable e zugewiesen | ||
| + | e = myFunction(7) # e = 10 | ||
| + | |||
| + | # a,b,e exisiteren noch im Speicher und können ausgegeben werden | ||
| + | print(' | ||
| + | print(a) | ||
| + | print(' | ||
| + | print(b) | ||
| + | print(' | ||
| + | print(e) | ||
| + | # c und d waren lokale Variablen und ergeben einen Fehler. | ||
| + | print(c) | ||
| + | print(d) | ||
| + | </ | ||
| + | Möchte man eine globale Variable innerhalb einer Funktion verändern, braucht es den Aufruf '' | ||
| + | |||
| + | === Assignment Operatoren === | ||
| + | <code python> | ||
| + | # In Ordnung | ||
| + | a = 3 | ||
| + | i = -3 | ||
| + | |||
| + | # Nicht in Ordnung. Wenn Vergleich, dann == | ||
| + | 3 = 4 | ||
| + | 3 = a | ||
| + | a + b = 3 | ||
| + | |||
| + | #In Ordnung | ||
| + | i = i + 1 | ||
| + | i += 1 # äquivalent zu oben | ||
| + | |||
| + | # es gibt ebenfalls +=, -=, *=, /=, %=, //=, **= | ||
| + | a %= 3 | ||
| + | a = a % 3 | ||
| + | </ | ||
| + | ==== L12 ==== | ||
| + | <code python afg8.py> | ||
| + | import random | ||
| + | |||
| + | def randomMatrix(zeilen, | ||
| + | matrix = [] | ||
| + | for i in range(zeilen): | ||
| + | zeile = [] | ||
| + | for j in range(spalten): | ||
| + | zeile.append(random.randint(0, | ||
| + | matrix.append(zeile) | ||
| + | zeile = [] | ||
| + | return matrix | ||
| + | </ | ||
| + | |||
| + | <code python afg9.py> | ||
| + | from gpanel import * | ||
| + | from math import * | ||
| + | makeGPanel(-50, | ||
| + | |||
| + | |||
| + | def drawNumbers(n): | ||
| + | for i in range(1, | ||
| + | # x und y Koordinate des Punktes. | ||
| + | # x = cos(winkel)*radius, | ||
| + | # y = sin(winkel)*radius | ||
| + | # der Winkel ist dabei 2*Pi, entspricht 360°, | ||
| + | # dividiert durch n mal die Zahl i. | ||
| + | x = cos(2*pi/ | ||
| + | y = sin(2*pi/ | ||
| + | move(x,y) | ||
| + | # Kreisfarbe | ||
| + | setColor(" | ||
| + | fillCircle(2) | ||
| + | # Textfarbe | ||
| + | setColor(" | ||
| + | text(x, | ||
| + | |||
| + | drawNumbers(20) | ||
| + | |||
| + | |||
| + | def checkPrime(i): | ||
| + | return((all(map(lambda x: i%x!=0, range(2, | ||
| + | |||
| + | </ | ||
| + | ==== L13 ==== | ||
| + | === Aufgaben Teil 1 === | ||
| + | Die Aufgaben Teil 1 sind korrigiert in One Note | ||
| + | === Aufgaben Teil 2 === | ||
| + | <code python afg7.py> | ||
| + | def dummyfunction(n): | ||
| + | for i in range(1, | ||
| + | print i | ||
| + | if i%15 == 0: #wichtig: zuerst modulo 15 teste! | ||
| + | print " | ||
| + | elif i%3 == 0: | ||
| + | print " | ||
| + | elif i%5 == 0: | ||
| + | print " | ||
| + | | ||
| + | dummyfunction(30) | ||
| + | </ | ||
| + | <code python afg8.py> | ||
| + | import random | ||
| + | |||
| + | def randomMatrix(zeilen, | ||
| + | matrix = [] | ||
| + | for i in range(zeilen): | ||
| + | zeile = [] | ||
| + | for j in range(spalten): | ||
| + | zeile.append(random.randint(0, | ||
| + | matrix.append(zeile) | ||
| + | zeile = [] | ||
| + | return matrix | ||
| + | |||
| + | #i | ||
| + | meineMatrix = randomMatrix(3, | ||
| + | # ii | ||
| + | def summe(matrix): | ||
| + | sumvar = 0 | ||
| + | # durch matrix loopen | ||
| + | for i in range(len(matrix)): | ||
| + | for j in range(len(matrix[i])): | ||
| + | # jedes element addieren | ||
| + | sumvar += matrix[i][j] | ||
| + | return sumvar | ||
| + | #iii | ||
| + | def durchschnitt(matrix): | ||
| + | sumvar = 0 | ||
| + | count = 0 | ||
| + | # durch matrix loopen | ||
| + | for i in range(len(matrix)): | ||
| + | for j in range(len(matrix[i])): | ||
| + | # jedes element addieren und counter erhöhen | ||
| + | sumvar += matrix[i][j] | ||
| + | count += 1 | ||
| + | return sumvar/ | ||
| + | |||
| + | |||
| + | #iv | ||
| + | print(durchschnitt(randomMatrix(100, | ||
| + | #oder | ||
| + | print(summe(randomMatrix(100, | ||
| + | # der durchschnitt nähert sich 500 | ||
| + | </ | ||
| + | <code python afg9.py> | ||
| + | from gpanel import * | ||
| + | from math import * | ||
| + | makeGPanel(-50, | ||
| + | #i | ||
| + | def checkPrime(n): | ||
| + | isPrime = True | ||
| + | for j in range(2, | ||
| + | if n%j == 0: | ||
| + | isPrime = False | ||
| + | break | ||
| + | return(isPrime) | ||
| + | |||
| + | |||
| + | def drawNumbers(n): | ||
| + | for i in range(1, | ||
| + | # x und y Koordinate des Punktes. | ||
| + | # x = cos(winkel)*radius, | ||
| + | # y = sin(winkel)*radius | ||
| + | # der Winkel ist dabei 2*Pi, entspricht 360°, | ||
| + | # dividiert durch n mal die Zahl i. | ||
| + | x = cos(2*pi/ | ||
| + | y = sin(2*pi/ | ||
| + | move(x,y) | ||
| + | # Kreisfarbe | ||
| + | # Aufgaben Teil ii) | ||
| + | if(checkPrime(i)): | ||
| + | setColor(" | ||
| + | else: | ||
| + | setColor(" | ||
| + | fillCircle(2) | ||
| + | # Textfarbe | ||
| + | setColor(" | ||
| + | text(x, | ||
| + | # Aufgabe Teil iv) | ||
| + | for j in range(1, | ||
| + | if j % i == 0 or i%j == 0: | ||
| + | xn = cos(2*pi/ | ||
| + | yn = sin(2*pi/ | ||
| + | line(x, | ||
| + | else: | ||
| + | continue | ||
| + | |||
| + | drawNumbers(30) | ||
| + | |||
| + | </ | ||