Dieser Bereich basiert auf dem Buch „Teach your kids to code“ from Bryson Pane und dem folgenden Udemy Kurs
https://www.udemy.com/course/teach-your-kids-to-code
Dieser Kurs ist zwar auf Englisch, aber sehr leicht zu verstehen.
Dauer: Mo. 28.8 bis Fr. 1.9 Von 9:00 bis 13:00
Teilnehmer: Felix G, Markus, Paul, Felix E
Installation: Python für Windows
Eingabe in einen Python Interpreter
>>>1+2+3
6
>>>print("Hallo Du")
Hallo Du
Eingabe in einen Python Interpreter
>>>1 + 1 2 >>>3 - 2 1 >>>4 * 4 16 >>>8 / 4 2.0 >>>7 / 4 1.75 >>>7 // 4 1 >>>7 % 4 3 >>>4 ** 2 16 >>>8 - 2 * 3 2 >>>( 8 - 2 ) * 3 18
Eingabe der Python IDLE und Ausführen (mit F5)
# YourName.py
name = input("What is your name?\n")
print("Hi, ", name, name, name, name, name)
Bildschirmausgabe:
What is your name? Wolfgang Hi, Wolfgang Wolfgang Wolfgang Wolfgang Wolfgang
Unser erstes Ziel, eine sechseckige Spirale zeichnen lassen. Dieses Programm zeigt erstmal nur unser erstes Ziel. Das Programm muss so noch nicht verstanden werden.
#NiceHexSpiral.py
import turtle
colors=['red', 'purple', 'blue',
'green', 'yellow', 'orange']
t=turtle.Pen()
t.speed(0)
turtle.bgcolor('black')
for x in range(360):
t.pencolor(colors[x%6])
t.width(x/100+1)
t.forward(x)
t.left(59)
#Viereck.py
import turtle
t=turtle.Pen()
#Erstes Viereck
t.forward(200)
t.left(90)
t.forward(200)
t.left(90)
t.forward(200)
t.left(90)
t.forward(200)
t.left(90)
#Zweites Viereck in rot
t.pencolor("red")
for x in range(4):
t.forward(100)
t.left(90)
#Und ein Dreieck in grün
t.pencolor("green")
for x in range(3):
t.forward(300)
t.left(120)
Wir verwenden die Library (Package) turtle - wird mit import gemacht
Bei einer Schleife muss eine Einrückung gemacht werden
# ViereckigeSpirale.py
import turtle
# Verwendung des Stifts von der turtle Library
t = turtle.Pen()
# Mit der Schleife wird die Spirale gezeichnet
# x ist die Schleifenvariable, wird immer groesser und damit der Strich laenger, der gezeichnet wird
for x in range(100):
# Zeichnen der Linie
t.forward(x)
# Drehen um 90 Grad
t.left(90)
Beschleunigen des Skripts, einfuegen der Geschwindigkeit (nach turtle.Pen)
t.speed(0)
Ändern der Spirale
Ändern des Abstands
t.forward(2*x)\\
Ändern der Farbe
t.pencolor("red")
Ändern der Lage
t.left(91)
# KreisSpirale.py
import turtle
# Verwendung des Stifts von der turtle Library
t = turtle.Pen()
t.speed(0)
t.pencolor("red")
# Mit der Schleife wird die Spirale gezeichnet
# x ist die Schleifenvariable, wird immer groesser und damit der Strich laenger, der gezeichnet wird
for x in range(100):
# Zeichnen der Linie
t.circle(x)
# Drehen um 90 Grad
t.left(91)
Ändern auf 90 und dann auf 60 Grad
Ändern der Farben auf andere Werte
„black“ (schwarz)
„white“ (weiss)
„red“ (rot)
„green“ (grün)
„blue“ (blau)
„orange“ (orange)
„yellow“ (gelb)
„purple“ (lila)
„pink“ (pink)
„brown“ (braun)
„gray“ (grau)
# FaerbigeViereckigeSpirale.py
import turtle
t = turtle.Pen()
t.speed(0)
# Eine Liste von Farben
colors = ["red", "yellow", "blue", "green"]
for x in range(100):
t.pencolor(colors[ x % 4 ])
t.forward(2*x)
t.left(91)
Was passiert wenn die Farben geändert werden?
Was passiert wenn die Zahl 4 geändert wird?
Wie kann die Hintergrundfarbe geändert werden?
Wie können Kreise gezeichnet werden?
Was passiert wenn der Winkel von 91 auf 95 erhöht wird?
Verwendung einer Variable
Mehr Farben
Andere Berechnungen
Eine zusätzliche Drehung am Ende
# FaerbigeViereckigeSpirale2.py
import turtle
t = turtle.Pen()
turtle.bgcolor("black")
t.speed(0)
# Eine Liste von Farben
sides = 10
colors = ["red", "yellow", "blue", "orange", "green",
"purple", "gray", "white", "pink", "light blue"]
for x in range(360):
t.pencolor(colors[ x % sides ])
t.forward(x * 3 / sides + x)
t.left(360/sides +1)
t.width(x*sides/100)
t.left(90)
Ein Beispiel mit Textvariablen
# ThankYou.py
my_name = "Bryson"
my_age = 43
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")
print("Thank you for taking this course,", your_name, "!")
Bildschirmausgabe
What is your name? Wolfgang How old are you? 52 My name is Bryson , and I am 43 years old. Your name is Wolfgang , and you are 52 . Thank you for taking this course, Wolfgang !
Beispiel Berechnung einer Pizzabestellung
#Pizza.py
# Ask the person how many pizzas they want, get the number with eval()
number_of_pizzas = eval(input("How many pizzas do you want? "))
# Ask for the menu cost of each pizza
cost_per_pizza = eval(input("How much does each pizza cost? "))
# Calculate the total cost of the pizzas as our subtotal
subtotal = number_of_pizzas * cost_per_pizza
# Wir geben 10% Trinkgeld
tip_rate = 0.1 # Store 10% as the decimal value 0.1
trinkgeld = subtotal * tip_rate
# Add the sales tax to the subtotal for the final total
total = subtotal + trinkgeld
# Show the user the total amount due, including tax
print("The total cost is €", total)
print("This includes €", subtotal, "for the pizza and")
print("€", trinkgeld, "als Trinkgeld.")
Bildschirmausgabe
How many pizzas do you want? 3 How much does each pizza cost? 8 The total cost is € 26.4 This includes € 24 for the pizza and € 2.4000000000000004 als Trinkgeld.
Wiederholte Zeichenausgabe
# SayMyName.py
# Ask the user for their name
name = input("What is your name? ")
# Print their name 100 times
for x in range(100):
# Print their name followed by a space, not a new line
print(name, end = " rules! ")
Bildschirmausgabe
What is your name? Wolfgang Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules! Wolfgang rules!
Textspirale
# SpiralMyName.py - prints a colorful spiral of the user's name
import turtle # Set up turtle graphics
t = turtle.Pen()
t.speed(0)
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]
# Ask the user's name using turtle's textinput pop-up window
your_name = turtle.textinput("Enter your name", "What is your name?")
# Draw a spiral of the name on the screen, written 100 times
for x in range(100):
t.pencolor( colors[ x % 4] )
t.penup()
t.forward( x * 4 )
t.pendown()
t.write(your_name, font = ("Times", int( (x + 4) / 4), "bold") )
t.left(92)
print("MathHomework.py")
# Ask the user to enter a math problem
problem = input("Enter a math problem, or 'q' to quit: ")
# Keep going until the user enters 'q' to quit
while (problem != "q"):
# Show the problem, and the answer using eval()
print("The answer to ", problem, "is:", eval(problem) )
# Ask for another math problem
problem = input("Enter another math problem, or 'q' to quit: ")
# This while loop will keep going until you enter 'q' to quit
Bildschirmausgabe
MathHomework.py Enter a math problem, or 'q' to quit: 5 + 2 The answer to 5 + 2 is: 7 Enter another math problem, or 'q' to quit: 5 / 2 The answer to 5 / 2 is: 2.5 Enter another math problem, or 'q' to quit: 5 // 2 The answer to 5 // 2 is: 2 Enter another math problem, or 'q' to quit: 5 % 2 The answer to 5 % 2 is: 1 Enter another math problem, or 'q' to quit: (4 + 2) * (2 + 2) The answer to (4 + 2) * (2 + 2) is: 24 Enter another math problem, or 'q' to quit: q
# ColorSpiralInput.py
import turtle
t = turtle.Pen()
t.speed(0)
turtle.bgcolor("black")
# Set up a list of any 8 valid Python color names
colors = ["red", "yellow", "blue", "green",
"orange", "purple", "white", "gray"]
# Ask the user for the number of sides
sides = int( turtle.numinput("Number of sides",
"How many sides do you want (1-8)?", 4, 1, 8))
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
t.pencolor( colors[x % sides] )
t.forward( x * 3 / sides + x)
t.left(360 / sides + 1)
t.width(x * sides / 200)