Rennpferde beim Sportwetten

Rennpferde beim Sportwetten

Aufgabe für Dictionary Comprehensions
Welches Rennpferd war zwischen 1970 und 1980 am längsten aktiv?
Hier sind die berümtsten Rennpferde ausgelistet:

1
print('Liste berühmter Rennpferde:')<br />race_horses = {<br />'Acatenango': (1993, 2005),<br />'Hoof Hearted': (1973, 1978),<br />'Seabiscuit': (1933, 1947),<br />'Anita Hanjab': (1951, 1969),<br />'Oil Beef Hooked': (1989, 1997),<br />'Ben Timover': (1974, 1986),<br />'Secretariat': (1972, 1989),<br />'Sea the Moon': (2014, 2020)<br />}<br />print('race_horses:', race_horses)

Schreiben Sie ein Programm, das mit Hilfe von Dictionary Comprehensions eine Auflistung der drei am längsten aktiv gewesenen Rennpferden:
{‚Pferdname1‘: n1, ‚Pferdname2‘: n2, ‚Pferdname3‘: n3}

Fragen zu Objektorientierte Programmierung

Fragen zu Objektorientierte Programmierung

  1. Was bedeutet Objektorientierte Programmierung?
  2. Auf welchen Prinzipien ist OOP basiert?
  3. Was sind Klassen und Objekte?
  4. Wie wird eine Klasse definiert und wie wird sie in der UML dargestellt?
  5. Was ist Konstruktor?
  6. Wie werden private und public Instanzvariablen erzeugt?
  7. Was sind Getter und Setter?
  8. Wie werden Objekte einer Klasse erzeugen?
  9. Wie kann man auf Attribute einer Klasse Zugreifen?
  10. Wie kann man Methoden einer Klasse aufrufen?
  11. Wie können Objekte mit variablen Anfangswerten erzeugt?
  12. Was sind Metaphern in Bezug zur OOP?
  13. Was sind Klassenattribute?
  14. Wie wird Polymorphie in Python realisiert?
  15. Wie ist die Relation zwischen Typ und Klasse?
  16. Was sind in Python Magische Methoden?
  17. Wie wird in Python die Vererbung realisiert und wie sie wird in der UML dargestellt?
  18. Was ist eine Hash-Funktion?
  19. Wie wird Mehrfachvererbung realisiert?
  20. Wa bedeutet Operator Overloading?
  21. Was sind ed-hoc-Objekte?

place()-Methode in Tkinter

Syntax:

widget.place(relx = 0.5, rely = 0.5, anchor = CENTER)

The Place geometry manager is the simplest of the three general geometry managers provided in Tkinter. It allows you explicitly set the position and size of a window, either in absolute terms, or relative to another window. You can access the place manager through the place() method which is available for all standard widgets. It is usually not a good idea to use place() for ordinary window and dialog layouts; its simply too much work to get things working as they should. Use the pack() or grid() managers for such purposes. 


GeSHi Error: GeSHi could not find the language lang (using path /homepages/0/d971837894/htdocs/clickandbuilds/wordpress_invario/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

Note : place() method can be used with grid() method as well as with pack() method.
When we use pack() or grid() managers, then it is very easy to put two different widgets separate to each other but putting one of them inside other is a bit difficult. But this can easily be achieved by place() method. In place() method, we can use in_ option to put one widget inside other.


GeSHi Error: GeSHi could not find the language lang (using path /homepages/0/d971837894/htdocs/clickandbuilds/wordpress_invario/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

Gehaltberechnung

Schreiben Sie ein Programm zur vereinfachten Berechnung der Steuer. Der Anwender wird dazu aufgefordert, sein monatliches Gehalt einzugeben. Anschließend werden 18 % dieses Betrags berechnet und ausgegeben. Nutzen Sie die Datei gehaltberechnung.py. Die Ausgabe kann zum Beispiel wie folgt aussehen:

Geben Sie Ihr Gehalt in Euro ein:
2500
Es ergibt sich eine Steuer von 450.0 Euro

Inch in Zentimeter Umwandler

Schreiben Sie ein Programm zur Eingabe und Umrechnung von beliebigen Inch-Werten in Zentimeter. Speichern Sie das Programm in der Datei inch_in_cm.py. Rufen Sie das Programm auf, und testen Sie es. Die Ausgabe kann zum Beispiel wie folgt aussehen:

Bitte geben Sie den Inch-Wert ein: 3.5
3.5 Inch sind 8.89 cm

Spiel: Rechnen trainiren

Probieren Sie den gegebenen Algorithmus mit allen mathematischen Operatoren aus

1
2
3
4
5
6
7
8
9
a=5
b=3
c=a+b
print(f"Die Aufgabe: {a} + {b}")
print("Bitte Lösungsvorschlag eingeben:")
z = input()
zahl = int(z)
print("Ihre Eingabe:", z)
print("Das Ergebnis:", c)

Weihnachtsbaum-1

Schreibe ein Programm, das die folgenden Muster in der Konsole ausgibt:


Lösung Tipps: String-Ausgabe mit Leerzeichen in print(), und benutzen Sie weitere parameter der Build-In Funktion print()

Fragen zur Selbstkontrolle

Fragen zur Selbstkontrolle

  1. Inwiefern ist der Satz »Schau nach vorne!« semantisch nicht eindeutig?
  2. Wie ändert man in Python Datentypen?
  3. Wie wird eine Variable mit Datentyp-Angabe deklariert?
  4. Wie wird Datentyp für Parameter in einer Funktion gegeben?
  5. In welcher Verhältnis stehen zu einander Ausdrucke und Literale?
  6. Zeichne ein Diagramm je für ein Verzweigung- und Schleife-Algorithmus.
  7. Was alles kann eine Zeichenkette sein? (welche Datentyp?)
  8. Welche Unterschied ist zwischen einem Script und einer Quellcode?

Python Grundlagen

Python Grundlagen

Grundbegriffe

Variablen
Anweisungen

Operatoren

mathematische Operatoren
logische Operatoren

Datentypen

int, float
str, bool

Datenstrukturen

list, tuple, set, dict

Slicing

[ : : -1]

Datenumwandlung

int(), str()

Kontrollstrukturen

Verzweigungen
Einseitige Verzweigung
Zeiseitige Verzweigung (if…else)
Fallunterscheidung (if…elif…else)
Bedingungen
Boolesche Werte
Boolesche Operatoren
Vergleichsketten
Bedingte Wiederholung – while
Endloswiederholung
Iteratoren for
Wiederholungen mit range()

Funktionen

Definition
Syntax
Funktionsnamen
Typangabe
Funktionsannotation
Parameter / Argumente
Optionale und voreingestellte (Default) Parameter
Funktion testen
Prozeduren
return-Anweisung
Positions- und Schlüßelwortargumente
Calltips
Docstring
Signatur
Build-In Funktionen
Globale und lokale Variablen
Rekursion

Joiner

Schreiben Sie eine Funktion join(values, delimiter), die eine Liste von Strings mit der übergebenen Trennzeichenfolge verbindet und als einen String zurückliefert. Implementieren Sie dies ohne Verwendung spezieller Python-Funktionalität selbst.

Beispiel:


Lösung

Zur Umwandlung des gegebenen Texts durchlaufen wir diesen zeichenweise von vorne nach hinten. Das Ergebnis sammeln wir in einem neuen String. Finden wir einen Vokal, fügen wir die übergebene Ersatzzeichenfolge ein, ansonsten den Konsonanten (bzw. genauer das Originalzeichen, was auch eine Ziffer oder ein Satzzeichen sein könnte):

1
2
3
4
5
6
7
8
def join(values, delimiter):
   result = ""
   for i, current_value in enumerate(values):
      result += current_value
      # Kein Trenner nach letztem Vorkommen
      if i < len(values) - 1:
         result += delimiter
   return result

Python-Shortcut Das Zusammenfügen von Strings lässt sich mit der geeigneten Funktion join()schön kompakt, verständlich und ohne Spezialbehandlung schreiben:


1
result = delimiter.join(values)

Vokale raten

Schreiben Sie eine Funktion translate_vowel(text, replacement), die in einem gegebenen Text alle Vokale durch ein Zeichen bzw. eine Zeichenfolge ersetzt. Das kann man etwa für ein kleines Ratequiz nutzen oder aber um Wortähnlichkeiten nur basierend auf den Konsonanten zu ermitteln.


Lösung

Zur Umwandlung des gegebenen Texts durchlaufen wir diesen zeichenweise von vorne nach hinten. Das Ergebnis sammeln wir in einem neuen String. Finden wir einen Vokal, fügen wir die übergebene Ersatzzeichenfolge ein, ansonsten den Konsonanten (bzw. genauer das Originalzeichen, was auch eine Ziffer oder ein Satzzeichen sein könnte):

1
2
3
4
5
6
7
8
9
10
def translate_vowel(text, replacement):
translated = ""
for letter in text:
if is_vowel(letter):
translated += replacement
else:
translated += letter
return translated
def is_vowel(letter):
return letter in "AÄEIOÖUüaäeioöuü"

Rock Paper Scissors game (Tekinter)

Game rules

Rock Paper Scissors is played by two players with their hands. In our project, one player will be the computer and the other player will be the user. The user will use a GUI window to play against the computer.

Let’s start with the rules. Each player has three choices at a time: rock, paper, or scissors.

Each player has to choose one of these three mutually exclusive options. If both players choose the same option, then that will be a tie. Hence, no one will get a point.

If user1 selects rock and user2 selects paper, then user2 wins that round. If user1 selects rock and user2 selects scissors, then user1 wins the round.

What if user1 selects paper and user2 selects scissors? Then, user2 wins that round.

A rough design of the game

We will create a GUI that has three buttons, one for “Rock”, one for “Paper”, and one for “Scissors”.

The user can click on any of these three buttons to select their choice and the computer will randomly select its choice.

We then compare both these choices and give one point to the winner of that round. We will display the points on the GUI window using a Text widget.

The following is a rough design of the GUI app that we are going to build.

We have mentioned the positions where we are planning to place the frames and widgets. The design is pretty simple so we can focus on the game logic.

Tip: Whenever you build a project, draw a rough design of your app on a piece of paper before you actually start coding. That will help you plan and give you an overview of the project. It is a good practice to follow in all personal coding projects, not just Tkinter projects.
Designing the Game UI

Creating a window
Adding Frames
Designing the first Frame
Designing the second Frame

Creating a window

1
2
3
4
5
6
7
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

window.mainloop()

Next, we will split this GUI into two parts using Frames.

Adding Frames

We will create two Frame widgets and put them in the GUI using the grid geometry.

We will place the first Frame on the left side of the window, at row 0 and column 0 , and the second Frame on the right side, at row 0 and column 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

frame1 = tk.Frame(window)
frame1.grid(column=0,row=0)

frame2 = tk.Frame(window)
frame2.grid(column=1,row=0)

window.mainloop()

Now, let’s place the widgets in these two Frames.

Designing the second Frame

We will start with designing the first Frame. Our plan is to place a Text widget here that will display the scores of the players (user and computer). Initially, we can keep their scores as “0”. We can also add some instructions to play the game in this Text widget. We will place this Text widget in row 0 and column 0 inside this Frame.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

frame1 = tk.Frame(window)
frame1.grid(column=0,row=0)

frame2 = tk.Frame(window)
frame2.grid(column=1,row=0)

text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
text_area.grid(column=0,row=0)
text_area.insert(tk.END,"\n\nYour Score : 0 \nComputer Score : 0 \n \nClick on any button to start.")

window.mainloop()

We have added some basic styling to the Text widget using options like

1
height
,
1
width
,
1
padx
and
1
pady
(for providing padding),
1
bg
, etc. We have used a hash code to give the background color. If you want some cool colors with hash codes, check out uicolorpicker.com and choose a color you like.We have inserted some text to display when the game starts. We will see how to update the scores later.

Designing the second Frame

Now, let’s design the second Frame and place the widgets. As per our initial design, we will have three buttons (Rock, Paper, and Scissors) on this Frame. We will place these buttons inside this Frame in rows 0, 1, and 2, while the column number will be 0.

We will give some horizontal and vertical padding between the two Frames to keep the UI clean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

frame1 = tk.Frame(window, padx=5, pady=5)
frame1.grid(column=0,row=0)

frame2 = tk.Frame(window)
frame2.grid(column=1,row=0)

text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
text_area.grid(column=0,row=0)
text_area.insert(tk.END,"\n\nYour Score : 0 \nComputer Score : 0 \n \nClick on any button to start.")

button1 = tk.Button(frame2,text="      Rock     ",bg="#50DBB4",padx=20,pady=25)
button1.grid(column=0,row=0)
button2 = tk.Button(frame2,text="     Paper    ",bg="#50DBB4",padx=20,pady=25)
button2.grid(column=0,row=1)
button3 = tk.Button(frame2,text="   Scissors  ",bg="#50DBB4",padx=20,pady=25)
button3.grid(column=0,row=2)

window.mainloop()

You can customize the UI further if you want. You can play with the widgets and options to make your UI unique and interesting.

Writing the Game Logic – Part 1
  • Defining the variables
  • Defining the computer choice method
  • Defining the rock, paper, and scissors methods
  • Connecting the methods and buttons

We have designed the game GUI in the previous lesson. Now, let’s write some Python code to implement the working of the game.

Defining the variables

First of all, we will create variables for user score, computer score, user choice, and computer choice.
We will initialize the scores with “0” and the choice variables with empty strings.

1
2
3
4
USER_SCORE = 0
COMP_SCORE = 0
USER_CHOICE = ""
COMP_CHOICE = ""

Defining the computer choice method#

We know that the user selects an option from Rock, Paper, or Scissors by clicking a button. But, how will the computer select its choice? Let’s create the method `random_computer_choice()’ to do that.
Inside this method, the computer will choose an option randomly and return it. We can import and use the random module in Python for this.

1
2
3
4
USER_SCORE = 0
COMP_SCORE = 0
USER_CHOICE = ""
COMP_CHOICE = ""

Defining the rock, paper, and scissors methods

Now, we will define three methods for the three buttons. When a user clicks a button, the corresponding method will be invoked. The method for the “Rock” button will assign the value

1
rock
to the
1
USER_CHOICE
variable. We will also call the
1
random_computer_choice()
method from this method for the computer to select its choice. We will pass these two choices to another method called
1
result()
, which we will define later.

1
2
3
4
5
6
def rock():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='rock'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

We made these variables global so that we can use them across the entire program.
Let’s define the methods for the “Paper” and “Scissors” buttons using the same logic.

1
2
3
4
5
6
7
8
9
10
11
12
def paper():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='paper'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)
def scissors():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='scissors'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

Connecting the methods and buttons#

Let’s put these variables and methods in our GUI app above the code for our widgets. That way, these methods will be available for the buttons to use.
We will link these methods with the buttons using the command option.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import random
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

USER_SCORE = 0
COMP_SCORE = 0
USER_CHOICE = ""
COMP_CHOICE = ""

def random_computer_choice():
    return random.choice(['rock','paper','scissors'])

def result(x,y):
    print("We will define it soon...")

def rock():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='rock'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

def paper():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='paper'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

def scissors():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='scissors'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

frame1 = tk.Frame(window, padx=5, pady=5)
frame1.grid(column=0,row=0,pady=5)

frame2 = tk.Frame(window)
frame2.grid(column=1,row=0)

text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
text_area.grid(column=0,row=0)
text_area.insert(tk.END,"\n\nYour Score : 0 \nComputer Score : 0 \n \nClick on any button to start.")

button1 = tk.Button(frame2,text="      Rock     ",bg="#50DBB4",padx=20,pady=25,command=rock)
button1.grid(column=0,row=0)
button2 = tk.Button(frame2,text="     Paper    ",bg="#50DBB4",padx=20,pady=25,command=paper)
button2.grid(column=0,row=1)
button3 = tk.Button(frame2,text="   Scissors  ",bg="#50DBB4",padx=20,pady=25,command=scissors)
button3.grid(column=0,row=2)

window.mainloop()

In the above code, we just created new methods and defined some variables. We did not add any functionality as of now. That is why the output is the same, and the buttons are not executable at the moment.
So, with that, we have the user choice and computer choice inputs ready. Now, we need to process these two variables and perform the calculations and updates.

Writing the Game Logic – Part 2
  • Accepting the input parameters
  • Writing the core logic of the game
  • Updating the scoreboard
  • Let’s run the app

Let’s define the result() method, which will implement the core logic of the game.

Accepting the input parameters
We will start by accepting the user choice and computer choice as parameters of the method.
Let’s also initialize the global variables which store the scores.

1
2
3
def result(user,comp):
    global USER_SCORE
    global COMP_SCORE

Writing the core logic of the game

The core game logic is the most important part of the game. Without this code, the game cannot run.
Take a look at the following table that shows all the different scenarios in the game. We will implement the logic to address all of these options.

User’s ChoiceComputer’s ChoiceWinner
RockRockTie
RockPaperComputer
RockScissorsUser
PaperRockUser
PaperPaperTie
PaperScissorsComputer
ScissorsRockComputer
ScissorsPaperUser
ScissorsScissorsTie
How can we implement this in Python? We will use conditional statements.
We will implement the logic by using if-elif-else conditions. Based on the winner, we will increment the score.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if(user==comp):
        print("Tie")
    elif(user=="rock"):
        if(comp=="scissors"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1
    elif(user=="paper"):
        if(comp=="rock"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1
    elif(user=="scissors"):
        if(comp=="paper"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1

Updating the scoreboard

We have written the logic to find the winner and update the score. Now, we need to update the scoreboard of the game, which is handled by a Text widget.
We will define a new Text widget and replace the original Text widget with this new one.
We can place the scores inside a string using string formatting. Finally, we will insert that string into the Text widget.

1
2
3
4
text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
text_area.grid(column=0,row=0)
answer = "\n\nYour Choice: {uc} \nComputer's Choice : {cc} \n\nYour Score : {u} \nComputer Score : {c} ".format(uc=USER_CHOICE,cc=COMP_CHOICE,u=USER_SCORE,c=COMP_SCORE)
text_area.insert(tk.END,answer)

Let’s run the app

Let’s place the

1
result()
method in the Tkinter code and run the app.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import random
import tkinter as tk

window = tk.Tk()
window.geometry("400x300")
window.title("Rock Paper Scissors Game")

USER_SCORE = 0
COMP_SCORE = 0
USER_CHOICE = ""
COMP_CHOICE = ""

def random_computer_choice():
    return random.choice(['rock','paper','scissors'])

def result(user,comp):
    global USER_SCORE
    global COMP_SCORE
    if(user==comp):
        print("Tie")
    elif(user=="rock"):
        if(comp=="scissors"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1
    elif(user=="paper"):
        if(comp=="rock"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1
    elif(user=="scissors"):
        if(comp=="paper"):
            print("You win")
            USER_SCORE+=1
        else:
            print("Comp wins")
            COMP_SCORE+=1
    text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
    text_area.grid(column=0,row=0)
    answer = "\n\nYour Choice: {uc} \nComputer's Choice : {cc} \n\nYour Score : {u} \nComputer Score : {c} ".format(uc=USER_CHOICE,cc=COMP_CHOICE,u=USER_SCORE,c=COMP_SCORE)
    text_area.insert(tk.END,answer)

def rock():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='rock'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

def paper():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='paper'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

def scissors():
    global USER_CHOICE
    global COMP_CHOICE
    USER_CHOICE='scissors'
    COMP_CHOICE=random_computer_choice()
    result(USER_CHOICE,COMP_CHOICE)

frame1 = tk.Frame(window, padx=5, pady=5)
frame1.grid(column=0,row=0)

frame2 = tk.Frame(window)
frame2.grid(column=1,row=0)

text_area = tk.Text(master=frame1,height=12,width=30,padx=10,pady=10,bg="#CAD5E2")
text_area.grid(column=0,row=0)
text_area.insert(tk.END,"\n\nYour Score : 0 \nComputer Score : 0 \n \nClick on any button to start.")

button1 = tk.Button(frame2,text="      Rock     ",bg="#50DBB4",padx=20,pady=25,command=rock)
button1.grid(column=0,row=0)
button2 = tk.Button(frame2,text="     Paper    ",bg="#50DBB4",padx=20,pady=25,command=paper)
button2.grid(column=0,row=1)
button3 = tk.Button(frame2,text="   Scissors  ",bg="#50DBB4",padx=20,pady=25,command=scissors)
button3.grid(column=0,row=2)

window.mainloop()

Congratulations, our Rock Paper Scissors game is ready! You can now play the game against the computer by running the code.

If you want to customize the app further, go ahead. Remember, experimenting with your app is the best way to learn Tkinter.

For example, you could try to add a new button called “Restart” in the GUI. When the user clicks the button, the scores will be set to 0, and the game will restart. Give it a try!

Grundrechenarten

Schreiben Sie eine Funktion calc(m, n), die zwei Variablen vom Typ int multipliziert,
das Produkt dann halbiert und den ganzzahligen Rest bezüglich der Division
durch 7 ausgibt.

Beispiel:


Als kleiner Hinweis zur Erinnerung hier nochmals: Bei einer Ganzzahldivision wird der Rest abgeschnitten, deswegen ergibt 25/2 als Ergebnis den Wert 12.

Lösung

Algorithmus Die Implementierung folgt einfach der mathematischen Operationen:

1
2
def calc(m, n):
   return m * n // 2 % 7

Statt des speziellen Operators // kann man auch eine Umwandlung des Ergebnisses der einfachen Division in eine Ganzzahl durch einen Aufruf von int() vornehmen:

1
2
def calc_v2(m, n):
   return int(m * n / 2) % 7

Tkinter OptionMenu(), Radiobutton() und CheckBox()

Creating OptionMenu widgets
Allgemeine Syntax für ein aufklappbares Menü:


myOptionMenu = tk.OptionMenu(parent, variable, default=None, *values)


Die OptionMenu() Methode erwartet parent, variable, default, and values als Parameter. Der parent-Parameter steht für window oder frame, in deren ihre Widgets platziert werden. 
Der variable-Parameter ist ein tk.StringVar – Objekt, der den Wert der ausgewählten Option des Menüs erfasst.
Als default -Parameter kann ein gewünstes Wert eigegeben werden, der immer am Anfang der Optionen-Liste erscheint. 
Der values -Parameter ist eine Liste mit Optionen. 


GeSHi Error: GeSHi could not find the language lang (using path /homepages/0/d971837894/htdocs/clickandbuilds/wordpress_invario/wp-content/plugins/codecolorer/lib/geshi/) (code 2)