Compare commits
10 Commits
11f3c7bc44
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f447c92e7 | |||
|
|
b7db925a10 | ||
|
|
e2b3c636ac | ||
|
|
fef4efd923 | ||
|
|
5b19a8626e | ||
|
|
7a0b826d95 | ||
|
|
de333a611c | ||
|
|
eee0183fc5 | ||
| 9d6cd786fe | |||
|
|
e5712b4f89 |
@@ -1,12 +0,0 @@
|
|||||||
# Python Basics - Code
|
|
||||||
|
|
||||||
## Deklarierung von Variablen
|
|
||||||
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<th>LOL</th>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>LOL</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
44
Aufgaben/Basic-Bot.py
Normal file
44
Aufgaben/Basic-Bot.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
import os
|
||||||
|
|
||||||
|
description = """
|
||||||
|
An example bot to showcase the discord.ext.commands extension module.
|
||||||
|
There are a number of utility commands being showcased here.
|
||||||
|
"""
|
||||||
|
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.members = True
|
||||||
|
intents.message_content = True
|
||||||
|
|
||||||
|
bot = commands.Bot(
|
||||||
|
command_prefix=commands.when_mentioned_or("!"),
|
||||||
|
description=description,
|
||||||
|
intents=intents)
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_ready():
|
||||||
|
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
||||||
|
print("------")
|
||||||
|
|
||||||
|
|
||||||
|
@bot.command()
|
||||||
|
async def add(ctx: commands.Context, left: int, right: int):
|
||||||
|
"""Adds two numbers together."""
|
||||||
|
await ctx.send(str(left + right))
|
||||||
|
|
||||||
|
|
||||||
|
@bot.command()
|
||||||
|
async def calc(ctx: commands.Context, left: int, opper: str, right: int):
|
||||||
|
if opper == "+":
|
||||||
|
await ctx.send(str(left + right))
|
||||||
|
elif opper == "-":
|
||||||
|
await ctx.send(str(left - right))
|
||||||
|
elif opper == "*":
|
||||||
|
await ctx.send(str(left * right))
|
||||||
|
elif opper == "/":
|
||||||
|
await ctx.send(str(left / right))
|
||||||
|
else:
|
||||||
|
await ctx.send(f"Bitte überprüfe deine Eingabe! `{opper}` ist kein gültiger Operand")
|
||||||
|
|
||||||
|
bot.run('')
|
||||||
@@ -28,4 +28,6 @@ Es findet unter anderem Anwendung in folgenden Bereichen:
|
|||||||
|
|
||||||
> **Beispiel:**
|
> **Beispiel:**
|
||||||
>
|
>
|
||||||
> `print("Hallo, Welt!")`
|
> `print("Hallo, Welt!")`
|
||||||
|
|
||||||
|
Quelle: [ w3schools ]( https://www.w3schools.com/python/python_intro.asp )
|
||||||
113
Einführung/03_Python-Basics-Code.md
Normal file
113
Einführung/03_Python-Basics-Code.md
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
# Python Basics - Code
|
||||||
|
|
||||||
|
## Deklarierung von Variablen
|
||||||
|
|
||||||
|
> *Variable mit Text*
|
||||||
|
>
|
||||||
|
> `var = "Hallo, Welt!"`
|
||||||
|
>
|
||||||
|
> ```
|
||||||
|
> >>> print(var)
|
||||||
|
> --> Hallo, Welt!
|
||||||
|
>
|
||||||
|
> >>> print(type(var))
|
||||||
|
> --> <class 'str'>
|
||||||
|
> ```
|
||||||
|
|
||||||
|
|
||||||
|
> *Variable mit nummerischem Wert*
|
||||||
|
>
|
||||||
|
> `var = 1024`
|
||||||
|
>
|
||||||
|
> ```
|
||||||
|
> >>> print(var)
|
||||||
|
> --> 1024
|
||||||
|
>
|
||||||
|
> >>> print(type(var))
|
||||||
|
> --> <class 'int'>
|
||||||
|
> ```
|
||||||
|
|
||||||
|
|
||||||
|
## Eingaben mit Python
|
||||||
|
> *Übergabe von Werten in eine Variable mit `input()`*
|
||||||
|
>
|
||||||
|
> `name = input()`
|
||||||
|
> ```
|
||||||
|
> >>> name = input("Bitte gib deinen Namen ein: ")
|
||||||
|
> --> print(name)
|
||||||
|
> ```
|
||||||
|
|
||||||
|
Die Variable "name" erhält den Wert, welcher bei der Eingabe eingegeben wird.
|
||||||
|
Typischerweise sind solche Eingaben meist von Datentyp String, also Text.
|
||||||
|
|
||||||
|
## Datenkonvertierung
|
||||||
|
Variablen können auch konvertiert werden. So können z.B. Integer (Ganzzahlen) immer zu String (Text) umgewandelt werden. Jedoch nicht andersherum!
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
|
||||||
|
> ```
|
||||||
|
> >>> var_int = 1024
|
||||||
|
> >>> print(type(var_int))
|
||||||
|
> --> <class 'int'>
|
||||||
|
>
|
||||||
|
> >>> var_int = str(1024)
|
||||||
|
> >>> print(type(var_int))
|
||||||
|
> --> <class 'str'>
|
||||||
|
> ```
|
||||||
|
Durch das Hinzufügen von `str()` weist man dem Wert klar einen Datentyp zu.
|
||||||
|
In diesem Fall String (Text).
|
||||||
|
|
||||||
|
Eingaben, welche über `input()` getätigt werden, sind meistens Strings.
|
||||||
|
Diese können jedoch, solange diese nur Zahlen enthalten, auch in Integer (Nummerische Werte) konvertiert werden.
|
||||||
|
|
||||||
|
> ```
|
||||||
|
> >>> var = input("Bitte eine Zahl eingeben: ")
|
||||||
|
> --> Bitte eine Zahl eingeben: 1024
|
||||||
|
> >>> print(type(var))
|
||||||
|
> --> <class 'str'>
|
||||||
|
>
|
||||||
|
> # Jetzt mit Konvertierung
|
||||||
|
> > >>> var = int(input("Bitte eine Zahl eingeben: "))
|
||||||
|
> --> Bitte eine Zahl eingeben: 1024
|
||||||
|
> >>> print(type(var))
|
||||||
|
> --> <class 'int'>
|
||||||
|
> ```
|
||||||
|
|
||||||
|
**Versucht man einer nummerischen Variable einen Textuellen-Datentyp zuzuweisen, wird das nicht funktionieren und das Programm wird mit einem Fehler gestoppt!**
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
> ```
|
||||||
|
> >>> var = int(input("Bitte eine Zahl eingeben: "))
|
||||||
|
> --> Bitte eine Zahl eingeben: Einhundert
|
||||||
|
> --> ValueError: invalid literal for int() with base 10: 'Einhundert'
|
||||||
|
> ```
|
||||||
|
|
||||||
|
Das Programm stoppt mit einem *ValueError*. Das bedeutet, dass der Wert einen Datentyp angenommen hat, welcher nicht gültig ist. Es wird auch gezeigt, mit welchem Datentyp gerechnet wird. In diesem Fall `int()`.
|
||||||
|
|
||||||
|
|
||||||
|
## Beispielaufgabe - Taschenrechner (addition)
|
||||||
|
> ```
|
||||||
|
> >>> zahl1 = int(input("Bitte 1. Zahl eingeben: "))
|
||||||
|
> --> Bitte 1. Zahl eingeben:
|
||||||
|
> >>> zahl2 = int(input("Bitte 2. Zahl eingeben: "))
|
||||||
|
> --> Bitte 2. Zahl eingeben:
|
||||||
|
> >>> summe = zahl1 + zahl2
|
||||||
|
> >>> print(summe)
|
||||||
|
> ```
|
||||||
|
|
||||||
|
**Programmiere nun einen Taschenrechner, welcher subtrahiert**
|
||||||
|
|
||||||
|
|
||||||
|
## String-Formatierung
|
||||||
|
Wenn du Variablen in deinem Code benutzt kannst du diese mit `print()` ausgeben.
|
||||||
|
Alternativ kannst du auch mit `print(var1, var2, varn)` mehrere Variablen ausgeben.
|
||||||
|
Möchtest du jedoch deinen String als Text flexibel formatieren kannst du sogenannte Platzhalter verwenden.
|
||||||
|
Schreibe in deinem `print()` nun folgendes: `print(f"")` mit {var} kannst du nun deine Variablen in den Text mit einbinden.
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
> ```
|
||||||
|
> >>> var1 = "Äpfeln"
|
||||||
|
> >>> var2 = "Saft"
|
||||||
|
> >>> print(f"Aus {var1} kann man {var2} machen")
|
||||||
|
> --> Aus Äpfeln kann man Saft machen
|
||||||
|
> ```
|
||||||
25
Einführung/04_Python-Basics-IF-ELSE.md
Normal file
25
Einführung/04_Python-Basics-IF-ELSE.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# Python Basics - Code (IF - ELSE)
|
||||||
|
Mit Hife von IF / ELSE Statements kann man bestimmte Aktionen triggern, wenn eine bestimmte Kondition erfüllt ist.
|
||||||
|
|
||||||
|
**Beispiel anhand einer Altersprüfung**
|
||||||
|
> ```
|
||||||
|
> name = str(input("Bitte gib deinen Namen ein: "))
|
||||||
|
> alter = int(input("Bitte gibt dein Alter ein: "))
|
||||||
|
>
|
||||||
|
> if alter >= 18:
|
||||||
|
> print(f"Hallo {name}, du bist volljährig ({alter})")
|
||||||
|
> else:
|
||||||
|
> print(f"Hallo {name}, du bist minderjährig ({alter})")
|
||||||
|
> ```
|
||||||
|
|
||||||
|
**Genereller Aufbau für IF-Anweisungen**
|
||||||
|
> ```
|
||||||
|
> >>> if cond1:
|
||||||
|
> >>> pass
|
||||||
|
> >>> elif cond2:
|
||||||
|
> >>> pass
|
||||||
|
> >>> elif condn:
|
||||||
|
> >>> pass
|
||||||
|
> >>> else:
|
||||||
|
> >>> pass
|
||||||
|
> ```
|
||||||
93
Einführung/05_Python-Basic-Discord-Bot.md
Normal file
93
Einführung/05_Python-Basic-Discord-Bot.md
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
# Python Basics - Discord Bot
|
||||||
|
|
||||||
|
Die Bibliothek welche wir für dieses Beisiel verwenden lautet [`py-cord`](https://pycord.dev/).
|
||||||
|
Du kannst pycord installieren indem du folgenden Befehl im Terminal ausführst: `python3 -m pip install py-cord`
|
||||||
|
|
||||||
|
## Aufbau des Codes
|
||||||
|
|
||||||
|
### Imports
|
||||||
|
An dieser Stelle importiert man alle, für das Projekt benötigten, Bibliotheken. Z.B.:
|
||||||
|
> ```
|
||||||
|
> import discord
|
||||||
|
> from discord.ext import commands
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Intents
|
||||||
|
Als Nächstes gibt man an, wie der Bot Nutzerdaten verarbeitet. In diesem Beispiel reichen die `defaults`:
|
||||||
|
|
||||||
|
> ```
|
||||||
|
> intents = discord.Intents.default()
|
||||||
|
> intents.members = True
|
||||||
|
> intents.message_content = True
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Bot Variable deklarieren
|
||||||
|
Hier wird die bot-Variable deklariert.
|
||||||
|
Außerdem wird noch der Prefix festgelegt (!), die Botbeschreibung wird hinzugefügt und die zuvor eingestellten intents werden übergeben.
|
||||||
|
> ```
|
||||||
|
> bot = commands.Bot(
|
||||||
|
> command_prefix=commands.when_mentioned_or("!"),
|
||||||
|
> description=description,
|
||||||
|
> intents=intents)
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Bot Events (@)
|
||||||
|
Sogenannte Events werden mit dem `@bot.event` Dekorator versehen. Dieser "Dekoriert" sozusagen den darunterliegenden Code.
|
||||||
|
Ein Beispiel für ein Bot-Event, wenn der Bot eingeloggt und bereit ist:
|
||||||
|
|
||||||
|
> ```
|
||||||
|
> @bot.event
|
||||||
|
> async def on_ready():
|
||||||
|
> print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
||||||
|
> print("------")
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Bot Commands
|
||||||
|
Um ein Command zu erstellen gibt es auch wieder einen Dekorator. Commands werden mit dem `@bot.command()` Dekorator versehen.
|
||||||
|
Ein Beispiel für einen Command um zwei Strings miteinander zu verbinden:
|
||||||
|
|
||||||
|
> ```
|
||||||
|
> @bot.command()
|
||||||
|
> async def add(ctx: commands.Context, left: int, right: int):
|
||||||
|
> """Adds two numbers together."""
|
||||||
|
> await ctx.send(str(left + right))
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Last but not least `bot.run()`
|
||||||
|
`bot.run("TOKEN")` kommt **immer** an das Ende deines Codes.
|
||||||
|
Dieser Codeschnipsel bildet den Abschluss.
|
||||||
|
|
||||||
|
|
||||||
|
### Das Beispiel einmal zusammengesetzt
|
||||||
|
```
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
description = """
|
||||||
|
An example bot to showcase the discord.ext.commands extension module.
|
||||||
|
There are a number of utility commands being showcased here.
|
||||||
|
"""
|
||||||
|
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.members = True
|
||||||
|
intents.message_content = True
|
||||||
|
|
||||||
|
bot = commands.Bot(
|
||||||
|
command_prefix=commands.when_mentioned_or("!"),
|
||||||
|
description=description,
|
||||||
|
intents=intents)
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_ready():
|
||||||
|
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
||||||
|
print("------")
|
||||||
|
|
||||||
|
|
||||||
|
@bot.command()
|
||||||
|
async def add(ctx: commands.Context, left: int, right: int):
|
||||||
|
"""Adds two numbers together."""
|
||||||
|
await ctx.send(str(left + right))
|
||||||
|
|
||||||
|
bot.run("TOKEN")
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user