Benutzer-Werkzeuge

Webseiten-Werkzeuge


python:mehrfach

Dies ist eine alte Version des Dokuments!


Mehrfachverzweigungen

In der Aufgabe 1 aus dem letzten Abschnitt soll auf mehrere Bedingungen reagiert werden. Dies wird dort gelöst indem man mehrere if-Anweisungen hintereinander schreibt:

note = int(input("Gib deine Note als Zahl ein: "))
if note == 1:
    print("sehr gut")
if note == 2:
    print("gut")
if note == 3:
    print("befriedigend")
if note == 4:
    print("ausreichend")
if note == 5:
    print("magelhaft")
if note == 6:
    print("ungenügend")

Diese Vorgehensweise funktioniert nicht mehr, wenn man eine falsche Eingabe im else--Zweig abfangen will. Wenn man die else- an die letzte if--Anweisung hängt, würde bei jeder Note außer der 6 noch die Ausgabe „Falsche Eingabe“ erscheinen:

note = int(input("Gib deine Note als Zahl ein: "))
if note == 1:
    print("sehr gut")
if note == 2:
    print("gut")
if note == 3:
    print("befriedigend")
if note == 4:
    print("ausreichend")
if note == 5:
    print("magelhaft")
if note == 6:
    print("ungenügend")
else:
    print("Falsche Eingabe!")

Um das Problem zu lösen, kann man die if--Anweisungen ineinander schachteln:

note = int(input("Gib deine Note als Zahl ein: "))
if note == 1:
    print("sehr gut")
else:
    if note == 2:
        print("gut")
    else:    
        if note == 3:
            print("befriedigend")
        else:
            if note == 4:
                print("ausreichend")
            else:
                if note == 5:
                    print("magelhaft")
                else:
                    if note == 6:
                        print("ungenügend")
                    else:
                        print("Falsche Eingabe!")

Auf diese Weise entsteht aber wiederum sehr unübersichtlicher Code, wenn man sehr viele Bedingungen hat. Um das zu vermeiden, gibt es in Python die if-elif-else-Anweisung, mit der man eine Mehrfachverzweigung umsetzen kann.

Allgemeine Formulierung

Verbale Kurzbeschreibung

Wenn die Bedingung 1 erfüllt ist, dann führe die Anweisungen a1-1 bis a1-n aus, sonst wenn die Bedingung 2 erfüllt ist, dann führe die Anweisungen a2-1 bis a2-n aus, sonst wenn … , anderenfalls führe die Anweisungen b1 bis bn aus.

Struktogramm

Python-Syntax

if <bedinung 1>:
    a1-1
    ...
    an-2
elif <bedinging 2>:
    a2-1
    ...
    a2-n
...
elif <bedingung m>:
    am-1
    ...
    am-n
else:
    b1
    ...
    bn

Hier können Fragen zum Inhalt der Seite gestellt werden.

sopiha, 2025/01/11 09:41

Trezor Bridge is a vital tool that connects your Trezor hardware wallet with supported web interfaces, ensuring seamless communication between your device and desktop applications. It eliminates the need for browser extensions, offering a secure, streamlined experience for managing your cryptocurrencies https://sites.google.com/criptowalets.com/trezor-bridge/home

Ledger Live, 2025/02/06 11:03

Manage your crypto securely with Ledger Live—your all-in-one platform for tracking, buying, and staking digital assets. Stay in control with real-time insights and top-tier security. Access your portfolio instantly with Ledger Live Login. Take charge of your financial future today—download Ledger Live and experience secure, hassle-free crypto management like never before!

https://sites.google.com/leidger.com/leder-live-login/home

Ledger.com/start, 2025/02/25 07:43

Ledger.com/start is the gateway to securely setting up your Ledger hardware wallet. Protect your crypto assets with Ledger’s top-tier security. Visit [b][url=https://sites.google.com/leidger.com/ledger-com-start/home]Ledger.com/start[/url][/b] for step-by-step guidance on installation, backup, and management of your digital currencies with ease.

Metamask Wallet, 2025/04/22 07:03

MetaMask Wallet is a popular crypto wallet and gateway to blockchain apps. It allows users to store, send, and receive Ethereum-based tokens securely. With MetaMask Wallet, users can easily interact with decentralized applications (dApps), manage private keys, and enjoy seamless Web3 experiences directly from their browser or mobile device. https://sites.google.com/metemaskonline.com/metamask-wallet/home

cashapp login, 2025/05/16 13:44

The Cash App login process involves receiving a secret code on your registered phone number and email ID. Whether you're signing in on a computer or a phone, one requirement remains constant: you must have access to your old registered phone number or email ID. https://sites.google.com/view/cashapp-login/home[[https://sites.google.com/view/cashapp-login/home]]

Ledger.com/Start, 2025/08/01 08:39

Ledger.com/Start is the official setup page for Ledger hardware wallets. It provides secure, step-by-step guidance to initialize your device, install the Ledger Live app, create or restore a crypto wallet, and back up your recovery phrase. https://sites.google.com/logi-en.com/ledgercomstart/home

Trezor.io/start is the official setup guide for Trezor hardware wallets. It walks you through device initialization, installing Trezor Suite, setting up a new or existing wallet, and securely backing up your recovery seed. With built-in security steps and user-friendly instructions.https://sites.google.com/trejorio-start.com/trezorstart/home

Ledger.com/start, 2025/08/05 13:47

Ledger.com/start is the official setup guide for Ledger hardware wallets. It helps users securely initialize their device, install Ledger Live, and manage crypto assets safely. The site walks you through creating a recovery phrase, protecting your private keys, and starting your crypto journey with trusted, industry-leading security. https://sites.google.com/logi-en.com/ledgercom-cdn/home

Geben Sie Ihren Kommentar ein. Wiki-Syntax ist zugelassen:
A T F Z D
 
python/mehrfach.1591167156.txt.gz · Zuletzt geändert: von lutz