2017-09-29 4 views
-2

Ich schrieb einen Code in ein Programmierbuch für Anfänger. So sieht es aus. (Ich habe diesen Beitrag zu verlängern, weil DIESER WEBSITE ÜBER DIE FRAGE BEITRÄGE SO pingelig IS.)Ich habe einen Syntaxfehler und ich habe keine Ahnung was zu tun ist

import random 
print("You are in a dark room in a mysterious castle.") 
print("In front of you are four doors. You must choose one.") 
playerChoice = input("Choose 1, 2, 3, or 4...") 
if playerChoice == "1": 
     print("You found a room full of tresure. YOU'RE RICH!!!") 
     print("GAME OVER, YOU WIN!") 
elif playerChoice == "2": 
     print("The door opens and an angry ogre hits you with his club.") 
     print("GAME OVER, YOU DIED!") 
elif playerChoice == "3": 
     print("You encounter a sleeping dragon.") 
     print("You can do either:") 
     print("1) Try to steal some of the dragon's gold") 
     print("2) Sneak around the dragon to the exit") 
     dragonChoice = input("type 1 or 2...") 
     if dragonChoice == "1": 
      print("The dragon wakes up and eats you. You are delicious.") 
      print("GAME OVER, YOU WERE EATEN ALIVE.") 
     elif dragonChoice == "2": 
      print("You sneak around the dragon and escape the castle, blinking in the sunshine.") 
      print("GAME OVER, YOU ESCAPED THE CASTLE.") 
     else: 
       print("Sorry, you didn't enter 1 or 2.") 

elif playerChoice == "4": 
      print("You enter a room with a sphinx.") 
      print("It asks you to guess what number it is thinking of, betwwen 1 to 10.") 
      number = int(input("What number do you choose?") 
     if number == random.randint (1, 10) 
       print("The sphinx hisses in dissapointment. You guessed correctly.") 
       print("It must let you go free.") 
       print("GAME OVER, YOU WIN.")    
     else: 
      print("The sphinx tells you that your guess is incorrect.") 
      print("You are now it's prisoner forever.") 
      print("GAME OVER, YOU LOSE.")   
     else: 
     print("sorry, you didn't enter 1, 2, 3, or 4...") 
     print("YOU TURN BACK AND LEAVE (YOU COWARD)") 
+0

Pls den Einzug Ihres Codes zu beheben, was es sieht tatsächlich aus wie in Ihrem Programm . – schwobaseggl

+0

Welchen Fehler bekommen Sie? Fügen Sie die gesamte Eingabe und Ausgabe hinzu. – Alec

Antwort

0

Hier sind zwei Ihrer Probleme:

number = int(input("What number do you choose?") 
if number == random.randint (1, 10) 

Die Besetzung zu int ist eine schließende Klammer fehlt und die if Anweisung einen Doppelpunkt fehlt.

Das letzte Problem hat mit der doppelten else Anweisung am Ende zu tun. Heben Sie die letzte Einrückung auf, vorausgesetzt, dass dies das ist, was Sie wollten.

Korrigiert:

import random 
print("You are in a dark room in a mysterious castle.") 
print("In front of you are four doors. You must choose one.") 
playerChoice = input("Choose 1, 2, 3, or 4...") 
if playerChoice == "1": 
    print("You found a room full of tresure. YOU'RE RICH!!!") 
    print("GAME OVER, YOU WIN!") 
elif playerChoice == "2": 
    print("The door opens and an angry ogre hits you with his club.") 
    print("GAME OVER, YOU DIED!") 
elif playerChoice == "3": 
    print("You encounter a sleeping dragon.") 
    print("You can do either:") 
    print("1) Try to steal some of the dragon's gold") 
    print("2) Sneak around the dragon to the exit") 
    dragonChoice = input("type 1 or 2...") 
    if dragonChoice == "1": 
     print("The dragon wakes up and eats you. You are delicious.") 
     print("GAME OVER, YOU WERE EATEN ALIVE.") 
    elif dragonChoice == "2": 
     print(
      "You sneak around the dragon and escape the castle, blinking in the sunshine.") 
     print("GAME OVER, YOU ESCAPED THE CASTLE.") 
    else: 
     print("Sorry, you didn't enter 1 or 2.") 

elif playerChoice == "4": 
    print("You enter a room with a sphinx.") 
    print("It asks you to guess what number it is thinking of, betwwen 1 to 10.") 

    number = int(input("What number do you choose?")) 

    if number == random.randint(1, 10): 
     print("The sphinx hisses in dissapointment. You guessed correctly.") 
     print("It must let you go free.") 
     print("GAME OVER, YOU WIN.") 
    else: 
     print("The sphinx tells you that your guess is incorrect.") 
     print("You are now it's prisoner forever.") 
     print("GAME OVER, YOU LOSE.") 
else: 
    print("sorry, you didn't enter 1, 2, 3, or 4...") 
    print("YOU TURN BACK AND LEAVE (YOU COWARD)") 
+0

Danke, ich habe getan, was du gesagt hast; aber ich erhalte jetzt Einzugfehler in diesem Abschnitt: if: number == random.randint (1, 10): drucken ("Die Sphinx zischt in Enttäuschung. Sie haben richtig geraten.") print ("Es muss dich gehen lassen kostenlos. ") print (" GAME OVER, SIE GEWINNEN. ") – NigelAwrey

+0

@NigelAwrey Ich aktualisierte meine Antwort, um den Code in seiner Gesamtheit einzuschließen, obwohl nur ein Teil davon Syntaxprobleme hatte. Ich habe getestet und es läuft gut. – Mark

0

Python ein Leerzeichen begrenzt Sprache ist, was bedeutet, Sie besonderes Augenmerk auf Einzug zahlen müssen. Ihr Einzug ist inkonsistent, was unweigerlich zu Problemen führen wird.

Ansonsten haben Sie nicht angegeben, was Ihr Fehler tatsächlich ist. Fügen Sie die Syntaxfehlerinformationen zu Ihrem Post hinzu.

0

Sie hatten nur ein paar Probleme mit Abstand und vergessen „:“ in Zeile 30

import random 
print("You are in a dark room in a mysterious castle.") 
print("In front of you are four doors. You must choose one.") 
playerChoice = input("Choose 1, 2, 3, or 4...") 
if playerChoice == "1": 
     print("You found a room full of tresure. YOU'RE RICH!!!") 
     print("GAME OVER, YOU WIN!") 
elif playerChoice == "2": 
     print("The door opens and an angry ogre hits you with his club.") 
     print("GAME OVER, YOU DIED!") 
elif playerChoice == "3": 
     print("You encounter a sleeping dragon.") 
     print("You can do either:") 
     print("1) Try to steal some of the dragon's gold") 
     print("2) Sneak around the dragon to the exit") 
     dragonChoice = input("type 1 or 2...") 
     if dragonChoice == "1": 
      print("The dragon wakes up and eats you. You are delicious.") 
      print("GAME OVER, YOU WERE EATEN ALIVE.") 
     elif dragonChoice == "2": 
      print("You sneak around the dragon and escape the castle, blinking in the sunshine.") 
      print("GAME OVER, YOU ESCAPED THE CASTLE.") 
     else: 
      print("Sorry, you didn't enter 1 or 2.") 

elif playerChoice == "4": 
      print("You enter a room with a sphinx.") 
      print("It asks you to guess what number it is thinking of, betwwen 1 to 10.") 
      number = int(input("What number do you choose?"))  
      if number == random.randint (1, 10): 
       print("The sphinx hisses in dissapointment. You guessed correctly.") 
       print("It must let you go free.") 
       print("GAME OVER, YOU WIN.")    
      else: 
       print("The sphinx tells you that your guess is incorrect.") 
       print("You are now it's prisoner forever.") 
       print("GAME OVER, YOU LOSE.")   
else: 
    print("sorry, you didn't enter 1, 2, 3, or 4...") 
    print("YOU TURN BACK AND LEAVE (YOU COWARD)") 
Verwandte Themen