/ / Verbindungscode in Python 3 - Windows, Python-3.x

Verbindungscode in Python 3 - Windows, Python-3.x

Gibt es eine Möglichkeit, "if direction == north:" mit "if direction = south" zu verbinden, ohne den Code zweimal kopieren und mehr Zeilen einfügen zu müssen?

 direction = input("do you want to travel north or south?")

if direction =="north":
print("You chose North")
print("An iron gate block your path")
print("You need a bronze key to open it")
print("You will have to go South")

if direction == "south":
print("You chose South")
time.sleep(1)
print("The path leads you to the Deep woods of Mirkwood")

Antworten:

0 für die Antwort № 1

Sie müssen möglicherweise definieren, was Sie mit "verbinden" meinen. Vielleicht ein Beispiel dafür liefern, was die Ausgabe für einen bestimmten Pfad bietet.

Wenn Sie den Südpfad anzeigen möchten, nachdem Sie Norden gewählt haben, können Sie etwas hinzufügen wie:

print("You will have to go South")
direction = "south"

Andernfalls, wenn Sie möchten, dass der Benutzer die Richtung nach der Auswahl des Nordens erneut auswählt, platzieren Sie die Auswahl in eine Schleife, die der folgenden ähnelt:

while True:
direction = input("do you want to travel north or south?")

if direction == "north":
#print statements
continue

if direction == "south":
#print statements
break