Benutzer-Werkzeuge

Webseiten-Werkzeuge


kicoku:kicokusommer2023

Dies ist eine alte Version des Dokuments!


KiCoKu Sommer 2023

Python

Installation: Python für Windows

Test der Installation

Eingabe in einen Python Interpreter

>>>1+2+3
   6
>>>print("Hallo Du")
   Hallo Du

Berechnungen

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

Text Ein und Ausgabe

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

Erstes Ziel Sechs Eckige Spirale

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)

Zeichnen von Dreieck und Viereck

#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)

Viereckige Spirale

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)

Aufgaben

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)

Kreise

# 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)

Verschiedene Farben

# 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)

Aufgaben

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?

Weitere Änderungen

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)

Python Abschnitt 3 - Variablen

Textvariablen

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 !

Zahlen Variablen

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.

Zahlen Variablen

kicoku/kicokusommer2023.1691999041.txt.gz · Zuletzt geändert: 2023/08/14 09:44 von newo