2017-09-29 134 views
-2

我正在爲初學者編寫一本編程書籍中的代碼。 這就是它的樣子。 (我必須擴展這個帖子因爲這個網站是如此的挑剔問題的帖子。)我有一個語法錯誤,我不知道該怎麼辦

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解決您的代碼的縮進它實際上看起來像在程序中, 。 – schwobaseggl

+0

你有什麼錯誤?包含完整的輸入和輸出。 – Alec

回答

0

這裏是你的問題二:

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

演員到int缺少一個右括號和if語句丟失一個冒號。

最後一個問題與最後的雙重else聲明有關。取消縮進最後一個,假設這是你想要的。

修正:

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

謝謝,我做了你所說的;但我現在在這一部分收到縮進錯誤:if:number == random.randint(1,10): print(「獅身人面像嘶嘶聲,失望,你猜對了。」) print(「It must let you go免費。「) print(」GAME OVER,YOU WIN。「) – NigelAwrey

+0

@NigelAwrey我更新了我的答案以包含代碼的全部內容,儘管只有一部分代碼存在語法問題。我測試過了,運行良好。 – Mark

0

Python是一個空格分隔的語言,這意味着你需要特別注意縮進。您的縮進不一致,這將不可避免地導致問題。

除此之外,你沒有指定你的錯誤實際是什麼。將語法錯誤信息添加到您的帖子。

0

你只是有一些問題,間距和忘記「:」在第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)") 
相關問題